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.
We can now add a page to our document. The following code is quite straightforward.
One point worth noting is the $_font_family check. For any text to be written to a page we need to set the font. However, we have to take into account the possibility that the font was set before any page was added, or that the font was set for a previous page in the current document. Either way we need to check the font class variable, and output the font information to the page. The function setFont() is used for this, which we shall cover later.
function addPage
() { $this->_page++; // Increment page count. $this->_pages[$this->_page] = ''; // Start the page buffer. $this->_state = 2; // Set state to page // opened. /* Check if font has been set before this page. */ if ($this->_font_family) { $this->setFont($this->_font_family, $this->_font_style, $this->_font_size); } }
Output of Simple Text As mentioned earlier, before any text can be output, font information must be supplied. We therefore need a function to define which font will be used. PDF specifications offer a core set of fonts which can be used with no extra information supplied to the PDF reader. You can also embed your own custom fonts into a PDF file, but for this you need to create font definitions, which are beyond the scope of this tutorial.
For now, limit your output to the following fonts:
The following method sets the font family name, and also (optionally) a style such as bold, italic or both, and a font size.
function setFont
($family, $style = '', $size = null) { $family = strtolower($family); if ($family == 'arial') { // Use helvetica. $family = 'helvetica'; } elseif ($family == 'symbol' || // No styles for $family == 'zapfdingbats') { // these two fonts. $style = ''; } $style = strtoupper($style); if ($style == 'IB') { // Accept any order $style = 'BI'; // of B and I. } if (is_null($size)) { // No size specified, $size = $this->_font_size; // use current size. } if ($this->_font_family == $family && // If font is already $this->_font_style == $style && // current font $this->_font_size == $size) { // simply return. return; } /* Set the font key. */ $fontkey = $family . $style; if (!isset($this->_fonts[$fontkey])) { // Test if cached. $i = count($this->_fonts) + 1; // Increment font $this->_fonts[$fontkey] = array( // object count and 'i' => $i, // store cache. 'name' => $this->_core_fonts[$fontkey]); } /* Store current font information. */ $this->_font_family = $family; $this->_font_style = $style; $this->_font_size = $size; $this->_current_font = $this->_fonts[$fontkey]; /* Output font information if at least one page has been * defined. */ if ($this->_page > 0) { $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->_current_font['i'], $this->_font_size)); } }
The following method enables easier changing between font sizes, without having to go through the whole setFont() function.
function setFontSize
($size) { if ($this->_font_size == $size) { // If already current return; // size simply return. } $this->_font_size = $size; // Set the font. /* Output font information if at least one page has been * defined. */ if ($this->_page > 0) { $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->_current_font['i'], $this->_font_size)); } }