Specifications
LISTING 30.5 Continued
// display a link to download
echo “download the pdf <a href = ‘hello.pdf’>here</a>”;
?>
The most likely error you will see if this script fails is the following:
Fatal error: Call to undefined function: pdf_open() in
/home/book/public_html/chapter30/pdftest.php on line 6
This means that you do not have the PDFlib extension compiled into PHP.
The installation is fairly straightforward, but some details change depending on the exact ver-
sions of PHP and PDFlib that you are using. A good place to check for detailed suggestions is
the user contributed notes on the PDFlib page in the annotated PHP manual.
When you have this script up and running on your system, it is time to look at how it works.
The first section of the code, including the line
$fp = fopen(“hello.pdf”, “w”);
creates a writeable file. It is worth noting that the code here is writing directly in to the current
directory even though we have already discussed a number of reasons why it is a bad idea to
have your permissions set up to allow PHP to write within the Web tree.
The line
$pdf = pdf_open($fp);
initializes a PDF document using the file we already opened. You can also call pdf_open()
without parameters to create a document in memory to be output directly to the browser. In
any case, you will need to capture the return value of
pdf_open(), as every subsequent call to
a PDF function will need it.
The function
pdf_set_info() enables you to tag the document with a subject, title, creator,
author, a list of keywords, and one custom, user-defined field.
Here we are setting a creator, author, and title. Note that all six info fields are optional.
pdf_set_info($pdf, “Creator”, “pdftest.php”);
pdf_set_info($pdf, “Author”, “Luke Welling and Laura Thomson”);
pdf_set_info($pdf, “Title”, “Hello World (PHP)”);
A PDF document consists of a number of pages. To start a new page, we need to call
pdf_begin_page(). As well as the identifier returned by pdf_open(), pdf_begin_page()
requires the dimensions of the page. Each page in a document can be a different size, but
unless you have a good reason not to, you should use a common paper size.
Generating Personalized Documents in Portable Document Format (PDF)
C
HAPTER 30
30
GENERATING
PERSONALIZED
DOCUMENTS IN
PDF
767
36 7842 CH30 3/6/01 3:40 PM Page 767