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

Writing Content - 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 will not be writing directly to the PDF file, the content is going to be buffered as it is created. Only after the PDF document is closed, and after some rearranging, will it be sent as a PDF file to the browser for download. So, as a first step, we will need to create a function to buffer the output. As it will be used internally within the PDF class, let's make it a private function.

function _out($s

if ($this->_state == 2) { 
->_pages[$this->_page] .= $s "n"
} else { 
->_buffer .= $s "n"

Here you can see straight away a number of class variables being used. Let's take a moment to work through them. The $_state variable keeps track of four different states that the PDF document can be in:
  • 0 = initialized
  • 1 = opened but no page opened
  • 2 = page opened
  • 3 = document closed

    The state is important in this method for determining how to buffer the output. If there is an open page, output is sent to the $_pages array. For any other state it is sent to the main buffer held in $_buffer variable.

    This distinction is necessary because page content is handled as a separate object within PDF and hence will need extra work on it when it is finally written to the main buffer.

    As you will later see, the $_state variable is used elsewhere to similarly add logic according to the document state.

    It is recommended to use the newline (ď\nĒ) following each output, as it is required in some cases (for example certain PDF instructions have to begin on a new line). Also, remember that PDF is case sensitive, so always follow the exact spelling of PDF syntax.

    Starting the Document
    The following two lines of code are required for initializing the document. These two lines must be called before any output:

    function open() 
    ->_state 1;          // Set state to initialized. 
        $this->_out('%PDF-1.3');    // Output the PDF header. 

    The second line writes the initial header that is required to identify the file and the PDF version being followed. The version number helps PDF readers handle the file properly.

    This tutorial will not be covering anything exotic, so you might as well stick with version 1.3. If you do start incorporating the more advanced PDF features found in 1.5 you will need to change the version number.

    >>> 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: