Home arrow Zend arrow Page 5 - PDFs with PHP part 1

Adding a Page - Zend

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.

  1. PDFs with PHP part 1
  2. Prerequisites
  3. The Factory Method
  4. Writing Content
  5. Adding a Page
  6. And Now to Output the Text
  7. Closing the Document
  8. The Trailer
  9. Compression
  10. Resources
By: Zend
Rating: starstarstarstarstar / 9
January 06, 2004

print this article



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() 
->_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:

  • Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique;
  • Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique;
  • Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic;
  • Symbol;
  • ZapfDingbats.

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

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. 
    /* Set the font key. */ 

$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. */ 

->_font_family  $family
->_font_style   $style
->_font_size    $size
->_current_font $this->_fonts[$fontkey]; 
/* Output font information if at least one page has been 
     * defined. */ 

if ($this->_page 0) { 
->_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) { 
->_out(sprintf('BT /F%d %.2f Tf ET'

>>> More Zend Articles          >>> More By Zend

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Taking the Zend Certified PHP Engineer Exam:...
- Quick Introduction to PHP 5
- PHP SOAP Extension
- Improving Performance
- PDFs with PHP part 2
- PDFs with PHP part 1
- PHP at Lycos
- Build Database Interfaces

Developer Shed Affiliates


Dev Shed Tutorial Topics: