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 will cover only the basics, which hopefully will give you a good start. PDF has a vast set of features and possibilities which can not be covered in a short tutorial. If you need more than what is covered here, you might want to look at some similar yet more complete solutions available, such as the excellent work done by Olivier Plathey on the FPDF class (http://fpdf.org), on which this tutorial is based. Of course, you may wish to take your own route and for that there is also the PDF reference (be warned: it’s 1,172 pages!) Basic familiarity with using PHP classes is assumed. Knowledge of PDF file structure is not required, as all references are explained.
You need to have a fully functional PHP install (either PHP 4 or PHP 5 will work here) and a running web server to output the PDF file from your script.
Acrobat Reader, XPDF, or an equivalent is required to see 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 The best approach is to set the code up as a class. This allows for greater flexibility later.
The primary (public) methods deal with the main operations on a PDF document: setting it up, adding pages, setting font, adding text, activating compression, and output of the document.
We shall review the various methods and features of the PDF language, and then eventually put it all together as one class.
Setting up Class Variables We will need a few class variables to keep track of output, pages, objects, settings, etc.
The following is a list of the essential variables, with brief comments. You will later see each one of these variables in its context, which will give you a better idea of how they are used. For now just briefly get yourself acquainted with them. var $_buffer = ''; // Buffer holding in-memory PDF. var $_state = 0; // Current document state. var $_page = 0; // Current page number. var $_n = 2; // Current object number. var $_offsets = array(); // Array of object offsets. var $_pages = array(); // Array containing the pages. var $_w; // Page width in points. var $_h; // Page height in points var $_fonts = array(); // An array of used fonts. var $_font_family = ''; // Current font family. var $_font_style = ''; // Current font style. var $_current_font; // Array with current font info. var $_font_size = 12; // Current font size in points. var $_compress; // Flag to compress or not. var $_core_fonts = array('courier'=> 'Courier', 'courierB'=> 'Courier-Bold', 'courierI'=> 'Courier-Oblique', 'courierBI'=> 'Courier-BoldOblique', 'helvetica'=> 'Helvetica', 'helveticaB'=> 'Helvetica-Bold', 'helveticaI'=> 'Helvetica-Oblique', 'helveticaBI'=> 'Helvetica-BoldOblique', 'times'=> 'Times-Roman', 'timesB'=> 'Times-Bold', 'timesI'=> 'Times-Italic', 'timesBI'=> 'Times-BoldItalic', 'symbol'=> 'Symbol', 'zapfdingbats' => 'ZapfDingbats');