Chapter 32 Generating Personalized Documents in Portable Document Format (PDF)
This approach works very well. The calls to
run very quickly, even
though the template and therefore the contents of
are fairly long.The main
problem from the point of view of this application is that the user will load the certifi-
cate in his word processor to print it.This is probably an invitation for people to modify
the output. RTF does not allow you to make a read-only document.
Generating a PDF Certificate from a Template
The process of generating a PDF certificate from a template is similar.The main differ-
ence is that when you create the PDF file,some of the placeholders might be interspersed
with formatting codes,depending on the version of Acrobat you are using.For example,if
you look in the certificate template file you have created (using a text editor),you can see
that the placeholders now look like this:
If you look through the file, you will see that,unlike RTF,this is not a format that
humans can easily read through.
The format of the PDF template file may vary depending on the version of Acrobat or other PDF generation
tool you are using. The code supplied in this example may not work as written when you generate your own
templates. Check your template and alter the code to suit. If you still have problems, use the PDFlib example
given later in the chapter.
There are a few different ways you can deal with this situation.You could go through
each of these placeholders and delete the formatting codes.Deleting them actually makes
little difference to how the document looks in the end because the codes embedded in the
previous template indicate how much space should be left between the letters of the place-
holders that you are going to replace anyhow.However,if you take this approach,you must
go through and hand-edit the PDF file and repeat this each time you change or update the
file.Doing all this work is not a big deal when you’re 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.
You can avoid this problem by 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
function to create a file,the
function to set the field values, and the
set the associated template form file.You can then pass this file back to the browser with
the appropriate MIME type—in this case,
—and the browser’s Acrobat Reader
plug-in should substitute the data into the form.