Zend
  Home arrow Zend arrow Page 4 - PDFs with PHP part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
ZEND

PDFs with PHP part 1
By: Zend
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2004-01-06


    Table of Contents:
  • PDFs with PHP part 1
  • Prerequisites
  • The Factory Method
  • Writing Content
  • Adding a Page
  • And Now to Output the Text
  • Closing the Document
  • The Trailer
  • Compression
  • Resources

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PDFs with PHP part 1 - Writing Content
    ( Page 4 of 10 )


    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) { 
            $this
    ->_pages[$this->_page] .= $s "n"
        
    } else { 
            $this
    ->_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() 
    {    
        $this
    ->_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
     

  •    

    ZEND ARTICLES

    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek