This tutorial is intended for the PHP programmer who needs to incorporate PDF generation in a script without using external libraries such as PDFlib (often unavailable due to licensing restrictions or lack of funds). This tutorial is the second of two parts, and builds on what was covered in the first part. Therefore, if you have not yet gone through Part 1, you are advised to do so (or at least read through it), before going through this tutorial (Part 2). Apart from what was dealt with in Part 1, no knowledge of PDF file structure is required to understand this tutorial, as all references are explained.
We saw in Part 1 how PDF files are, after all, just plain text files, with specific markup syntax that describes what should happen to objects within the document, such as text and images. We shall now further examine this syntax, to allow us to create a more complete PDF document (i.e more than simple text).
Learning Objectives With the combined knowledge from Part 1, and from the current tutorial (Part 2), you should be able to put together a simple PDF class that can: • Add colors to your PDF documents; • Add lines, rectangles and circles; • Insert JPEG images.
Prerequisites You need to have a fully functional PHP install (either PHP 4 or PHP 5), and a running web server to output the PDF file from your script.
Acrobat Reader, XPDF, or equivalent is required, to display the results of your work.
You do not need any external library, either separate or compiled into PHP, to generate your PDF files.
How It Works We already have the class from Part 1 of this tutorial. In Part 2 we will just add a few more class variables and methods to it.
We shall review the various methods and features of the PDF language, and then finally put it all together as one class.
New Class Variables In addition to the class variables we introduced in Part 1, we will also need the following. var $_fill_color = '0 g'; // Color used on text and fills. var $_draw_color = '0 G'; // Line draw color. var $_line_width = 1; // Drawing line width. var $_images = array(); // An array of used images. In the PDF specifications, black is the default value for fill and draw colors, and the default line width is 1 point. Therefore we will use these values as our class defaults for the above variables.