PDF is the Portable Document Format developed by Adobe. It’s an open standard implemented by Adobe in their Acrobat series of software, but implementable and extensible by anybody who’s got the time, inclination, and knack. One trick that’s got a lot of potential is using PHP to dynamically generate PDF files and serve them via the web.
PHP can do a lot for your web operation (read our PHP Tutorial for Beginners tutorial). You can generate nice-looking printable receipts, invoices, and brochures. Disc-Cover has a test site that looks up info about a CD automatically and then generates a PDF label for the CD box that you can print, cut out, and use. And there are literally one billion other possible uses for dynamically generated PDFs.
So what are you waiting for?
You have a variety of PDF-generation options. The standard, classic way of doing it is with PDFlib. Because it’s so widely used and well-integrated into PHP, that’s the library I’ll go over today. But it’s by no means the only way of doing things. PDFlib is source-available, but not free. The license specifies that PDFlib can be used and redistributed without charge for non-commercial projects, but commercial use carries a fee.
There are also a number of completely free options. These include R&OS and FPDF, Panda. The choice is yours. (I haven’t had a chance to test these free packages very thoroughly. If you have had negative or positive experiences with them, please do let me know.)
In order to install PDFlib, you first have to download the PDFlib source (precompiled binaries are available but they watermark their output). Uncompress the package and compile and install it according to the instructions for your platform. We are assuming that you already have a working build of PHP. Instructions for installing PDFlib are included with the download, but I’ll go over the instructions for Unixy systems (including Mac OS X) anyway.
This article may become dated in terms of version and patch information. In general, it’s a good idea to download the PDFlib on PHP manual (PDF link) and read the most current instructions before installing PDFlib.
Install all the files in their appropriate places according to the instructions.
In your PHP directory, rebuild the configure file:./buildconf
Rebuild PHP, adding the switch --with-pdflib=PDFDIR (change PDFDIR to the name of the directory where PDFlib is installed).
Restart Apache, and there you go.
PDFlib, in conjunction with PHP, has over a hundred functions available for creating PDFs. To conserve time, space, and sanity, I’ll only cover a few of these. The full gamut is explained both on the PHP site and in the documentation that accompanies the PDFlib download.
Let’s zip through a step-by-step explanation of creating a new PDF document. Nothing fancy, just a couple of lines of text on a page. To start with, we need to initialize a new PDF object with the pdf_new() function. We will assign the object to the variable $pdf. This object handle is how we’ll refer to the PDF we’re creating from now on.
$pdf = pdf_new();
Next we want to create a new blank PDF file. This is done with the PDF_begin_document() function. We tell it the name of the PDF object we’re working with:
Moving swiftly along:we’ve got a PDF object containing one blank PDF file. Now we start to fill it in. First let’s set the metadata fields for the PDF. This step is optional. Every PDF has header information in which the document title, creator, and so forth can be specified. We set this data with the pdf_set_info() function. This function takes three arguments:the PDF object we’re working with, the name of the property we want to set, and the value we want to set it to. The predefined property names you can use are Author, Creator, Keywords, Subject, and Title. So, for example:
pdf_set_info($pdf, "Author", "Paul Adams");
Next we create the first page of our PDF, specifying width and then height, in points. A point is 1/72 of an inch, so if we want an 8.5-by-11-inch page, we do a little multiplying:
pdf_begin_page($pdf, (72 * 8.5), (72 * 11));
(If you are following along with pencil and paper, you will see that we could have substituted “612″ and “792″ for the width and height terms.)
Now we’ve got everything set up. Next, let’s get to the meat of our PDF:the content. First we need to choose a font to display our text. This is a two-step process:locating the font, and then using it. The PDFlib manual is very helpful on this topic.
This section covers the use of a depricated function. Please help us keep Webmonkey up to date by editing it.
The function we use to locate and prepare the font is pdf_findfont(). It takes four arguments:the PDF object handle, the font name, the encoding of the font, and whether or not the font should be embedded in the document. We are going to use one of the 14 fonts built in to PDFlib, which means we don’t have to get our hands too dirty. It means we don’t have to embed the font (which we indicate by a “0″ value for the last argument), and the encoding is “host”. These 14 fonts are (still have that pencil ready?):Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Symbol, and ZapfDingbats. For our text we’ll use Times-Roman, okay? pdf_findfont() returns a font handle, which we’ll pass to the $font variable:
$font = pdf_findfont($pdf, "Times-Roman", "host", 0)
If something goes wrong, the font handle $font will be set to “FALSE”. But nothing ever goes wrong. Part Two is to tell the renderer that we want to use that font, and to specify the size in points, with:
pdf_setfont(). pdf_setfont($pdf, $font, 16);
Now we’re almost ready to print our text. We just need to set the position for it to appear, with pdf_set_text_pos(). With this function, we specify an X and Y coordinate in points, counting from the lower left corner of the page, which is (0,0). Let’s print our text near the upper left of the page:
pdf_set_text_pos($pdf, 72, 720);
pdf_show() is used to print text at the current position. Its usage is very simple:
pdf_show($pdf, "My First PDF Document");
There’s the text! At last! At this point, if we wanted, we could continue to move around the page, printing more text in various fonts, drawing images, adding new pages, and so forth. But I think we’ve covered about enough for an intro course. Let’s close things down.
We end the page with the pdf_end_page() function:
and then close the object:
The complete PDF document that we’ve created is now neatly put away in the PHP buffer. In order to display or save it, we need to fetch it from there and do something with it. The pdf_get_buffer() function gets the contents of the buffer and puts it into a new variable.
$document = pdf_get_buffer($pdf);
Now let’s create a new PDF — just kidding. Actually we can’t use any more PDF functions until we get rid of this object we’ve just pulled from the buffer. That’s a safeguard built into the system. We must either send the PDF to the user’s browser, save it as a file on the server, or delete it from memory. Let’s do the latter two.
To display it, we need to first send HTTP header information, to let the browser know what’s coming. This is done with the header() function. We need to send three headers:Content-Type, Content-Length, and Content-Disposition.
First let’s find the length of the PDF, and give it a filename:
$length = strlen($document); $filename = "myfirstpdf.pdf";
Then we output the headers:
header("Content-Type:application/pdf"); header("Content-Length:" . $length); header("Content-Disposition:inline; filename=" . $filename);
Now we’re ready to send the actual document to the browser, with a simple echo() function:
All set. Finally, as a last step, we purge the memory to get ready for our next adventure:
Here is a look at all that code assembled in one place:
<?php $pdf = pdf_new(); pdf_begin_document($pdf); pdf_set_info($pdf, "Author", "Paul Adams"); pdf_begin_page($pdf, (72 * 8.5), (72 * 11)); $font = pdf_findfont($pdf, "Times-Roman", "host", 0) pdf_setfont($pdf, $font, 16); pdf_set_text_pos($pdf, 72, 720); pdf_show($pdf, "My First PDF Document"); pdf_end_page($pdf); pdf_end_document($pdf); $document = pdf_get_buffer($pdf); $length = strlen($document); $filename = "myfirstpdf.pdf"; header("Content-Type:application/pdf"); header("Content-Length:" . $length); header("Content-Disposition:inline; filename=" . $filename); echo($document); unset($document); pdf_delete($pdf); ?>
That’s kind of a lot of code to print one line of text, it’s true. But that was just an example. The PDF format wasn’t invented for displaying single lines (although it does them so nicely!). You can use the framework above to output all kinds of complex PDFs, with logos and diagrams and color and links and whatever. There are simple PHP functions for all that stuff. Just start small and work step-by-step. The PDFlib package includes a very cool demo, called pdfclock, of an analog clock rendered as a dynamic PDF. See where that leads you. Follow your heart.