13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

796 Chapter 32 Generating Personalized Documents in Portable Document Format (PDF)<br />

After you have finished the whole PDF document, you need to close it by using<br />

pdf_close(). When you are generating a file, you also need to close the file.<br />

The line<br />

pdf_close($pdf);<br />

completes the generation of the Hello World document.<br />

Now you can send the completed PDF to the browser:<br />

$data = pdf_get_buffer($pdf);<br />

// generate the headers to help a browser choose the correct application<br />

header('Content-Type: application/pdf');<br />

header('Content-Disposition: inline; filename=testpdf.pdf');<br />

header('Content-Length: ' . strlen($data));<br />

// output PDF<br />

echo $data;<br />

You could also write this data to disk if you preferred. PDFlib allows you to do this by<br />

passing a filename as the second parameter to pdf_open_file().<br />

Note that some PDFlib function parameters that are documented in the <strong>PHP</strong> manual<br />

as being optional are required in some versions of PDFlib.The document for the certificate<br />

is more complicated, including a border, a vector image, <strong>and</strong> a bitmap image.With<br />

the other two techniques, you can add these features using a word processor.With<br />

PDFlib, you must add them manually.<br />

Generating a Certificate with PDFlib<br />

To use PDFlib, we chose to make some compromises for this project. Although it is<br />

almost certainly possible to exactly duplicate the certificate used previously, a lot more<br />

effort would be required to generate <strong>and</strong> position each element manually rather than use<br />

a tool such as Microsoft Word to help lay out the document.<br />

We want to use the same text as before, including the red rosette <strong>and</strong> the bitmap signature,<br />

but we are not going to try to duplicate the complex border.The complete code<br />

for this script is shown in Listing 32.6.<br />

Listing 32.6<br />

pdflib.php— Generating a Certificate Using PDFlib<br />

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

Saved successfully!

Ooh no, something went wrong!