Specifications
This is a neat way of doing things, but it has two limitations. First, it assumes that you own a
copy of Acrobat. Second, it is difficult to substitute in text that is inline rather than text that
looks like a form field. This might or might not be a problem, depending on what you are
trying to do. We have largely used PDF generation for generating letters where many things
must be substituted inline. FDFs do not work well for this purpose. If you are auto-filling for
example, a tax form online, this will not be a problem.
You can read more about the FDF format at Adobe’s site:
http://www.adobe.com/support/techdocs/16196.htm
You should also look at the FDF documentation in the PHP manual if you decide to use this
approach:
http://www.php.net/manual/ref.fdf.php
We turn now to our PDF solution to the previous problem.
We can still find and replace the placeholders in our PDF file if we recognize that the addi-
tional format codes consist solely of hyphens, digits, and parentheses and can therefore be
matched via a regular expression. We have written a function, pdf_replace(), to automatically
generate a matching regular expression for a placeholder and replace that placeholder with the
appropriate text.
Other than this addition, the code for generating the certificate via a PDF template is very
similar to the RTF version. This script is shown in Listing 30.4.
LISTING 30.4 pdf.php—Script to Produce Personalized PDF Certificate Via a Template
<?
set_time_limit( 180 ); // this script can be very slow
function pdf_replace( $pattern, $replacement, $string )
{
$len = strlen( $pattern );
$regexp = ‘’;
for ( $i = 0; $i<$len; $i++ )
{
$regexp .= $pattern[$i];
if ($i<$len-1)
$regexp .= “(\)\-{0,1}[0-9]*\(){0,1}”;
}
return ereg_replace ( $regexp, $replacement, $string );
}
Generating Personalized Documents in Portable Document Format (PDF)
C
HAPTER 30
30
GENERATING
PERSONALIZED
DOCUMENTS IN
PDF
763
36 7842 CH30 3/6/01 3:40 PM Page 763