
This approach works very well. The calls to str_replace() run very quickly, even though our
template and therefore the contents of $output are fairly long. The main problem from the
point of view of this application is that the user will load the certificate in his word processor
in order to print it. This is probably an invitation for people to modify the output. RTF does not
allow us to make a read-only document.
Generating a PDF Certificate from a Template
The process of generating a PDF certificate from a template is very similar. The main differ-
ence is that when we create the PDF file, some of our placeholders might be interspersed with
formatting codes. For example, if we look in the certificate template file we have created, we
can see that the placeholders now look like this:
<<N)-13(AME)-10(>)-6(>
<<Na)-9(m)0(e)-18(>>
<)-11(<)1(sc)-17(or)-6(e)-6(>)-11(>
<)-11(<)1(m)-12(m)0(/d)-6(d)-19(/)1(yy)-13(yy)-13(>>
If you look through the file, you will see that, unlike RTF, this is not a format that humans can
easily read through.
There are a few different ways we can deal with this.
We could go through each of these placeholders and delete the formatting codes. This actually
makes fairly little difference to how the document looks in the end as the codes embedded in
the previous template indicate how much space should be left between the letters of the place-
holders that we are going to replace anyhow. However, if we take this approach, we must go
through and hand edit the PDF file and repeat this each time we change or update the file. This
is not a big deal when dealing with only four placeholders, but it becomes a nightmare when,
for example, you have multiple documents with many placeholders, and you decide to change
the letterhead on all the documents.
We can avoid this problem using a different technique. You can use Adobe Acrobat to create a
PDF form—similar to an HTML form with blank named fields. You can then use a PHP script
to create what is called an FDF (Forms Data Format) file, which is basically a set of data to be
merged with a template. You can create FDFs using PHP’s FDF function library: specifically,
the fdf_create() function to create a file; the fdf_set_value() function to set the field
values; and the fdf_set_file() function to set the associated template form file. You can
then pass this file back to the browser with the appropriate MIME type, in this case vnd.fdf,
and the browser’s Acrobat Reader plug-in should substitute the data into the form.
Building Practical PHP and MySQL Projects
P
ART V
762
36 7842 CH30 3/6/01 3:40 PM Page 762
Comentarios a estos manuales