Zend
  Home arrow Zend arrow Page 4 - PDFs with PHP part 1
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
ZEND

PDFs with PHP part 1
By: Zend
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway