13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution Overview<br />

789<br />

There are a few different ways you can deal with this situation.You could go through<br />

each of these placeholders <strong>and</strong> delete the formatting codes. Deleting them actually makes<br />

little difference to how the document looks in the end because the codes embedded in the<br />

previous template indicate how much space should be left between the letters of the placeholders<br />

that you are going to replace anyhow. However, if you take this approach, you must<br />

go through <strong>and</strong> h<strong>and</strong>-edit the PDF file <strong>and</strong> repeat this each time you change or update the<br />

file. Doing all this work is not a big deal when you’re dealing with only four placeholders,<br />

but it becomes a nightmare when, for example, you have multiple documents with many<br />

placeholders, <strong>and</strong> you decide to change the letterhead on all the documents.<br />

You can avoid this problem by using a different technique.You can use Adobe<br />

Acrobat to create a PDF form—similar to an HTML form with blank, named fields.You<br />

can then use a <strong>PHP</strong> script to create what is called an FDF (Forms Data Format) file,<br />

which is basically a set of data to be merged with a template.You can create FDFs using<br />

<strong>PHP</strong>’s FDF function library: specifically, the fdf_create() function to create a file, the<br />

fdf_set_value() function to set the field values, <strong>and</strong> the fdf_set_file() function to<br />

set the associated template form file.You can then pass this file back to the browser with<br />

the appropriate MIME type—in this case, vnd.fdf—<strong>and</strong> the browser’s Acrobat Reader<br />

plug-in should substitute the data into the form.<br />

This way of doing things is neat, but it has two limitations. First, it assumes that you<br />

own a copy of Acrobat Professional (the full version, not the free reader, or even the<br />

St<strong>and</strong>ard edition). Second, it is difficult to substitute in text that is inline rather than text<br />

that looks like a form field.This might or might not be a problem, depending on what<br />

you are trying to do.We largely use PDF generation for generating letters where many<br />

things must be substituted inline. FDFs do not work well for this purpose. If you are<br />

auto-filling, for example, a tax form online, this will not be a problem.<br />

You can read more about the FDF format at Adobe’s site: http://www.adobe.com/<br />

devnet/acrobat/fdftoolkit.html.<br />

You should also look at the FDF documentation in the <strong>PHP</strong> manual if you decide to<br />

use this approach: http://www.php.net/manual/en/ref.fdf.php.<br />

We turn now to the PDF solution to the previous problem.<br />

You can still find <strong>and</strong> replace the placeholders in the PDF file if you recognize that<br />

the additional format codes consist solely of hyphens, digits, <strong>and</strong> parentheses <strong>and</strong> can<br />

therefore be matched via a regular expression.We wrote a function, pdf_replace(), to<br />

automatically generate a matching regular expression for a placeholder <strong>and</strong> replace that<br />

placeholder with the appropriate text.<br />

Note that with some versions of Acrobat, the placeholders are in plain text, <strong>and</strong> you<br />

can replace them with str_replace(), as you did before.<br />

Other than this addition, the code for generating the certificate via a PDF template is<br />

similar to the RTF version.This script is shown in Listing 32.4.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!