Zend
  Home arrow Zend arrow Page 7 - 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 - Closing the Document
    (Page 7 of 10 )


    The closing function is a bit more involved: we need to clean up a bit, set some PDF tags, and create a few references. This is the code that does most of the work in setting up the buffered content to finally look like a PDF file.

    Begin by checking that there is at least one page, and setting the state to “page closed”.

     
    function close() 

        
    if ($this->_page == 0) {    // If not yet initialised, add 
            $this->addPage();       // one page to make this a valid 
        }                           // PDF. 
        
    $this->_state 1;          // Set the state page closed.


    Now output the couple of objects that we have been buffering separately: pages and other resources. We shall go through each method later.
       


    /* Pages and resources. */ 
        $this
    ->_putPages(); 
        $this
    ->_putResources(); 



    Include some document information. PDF treats this information as a separate object, and here we introduce the _newobj() function. You could add other information to this section, such as author, subject, title, keywords, etc. For now we'll just put in the producer.
       


    /* Print some document info. */ 
        $this
    ->_newobj(); 
        $this
    ->_out('<<'); 
        $this
    ->_out('/Producer (My First PDF Class)'); 
        $this
    ->_out(sprintf('/CreationDate (D:%s)'
                            date
    ('YmdHis'))); 
        $this
    ->_out('>>'); 
        $this
    ->_out('endobj'); 


    The next section is the PDF catalog, which defines how the document will initially look in the reader. You can take this as it is for now. There’s nothing exciting going on here, but it is needed.
       


    /* Print catalog. */ 
        $this
    ->_newobj(); 
        $this
    ->_out('<<'); 
        $this
    ->_out('/Type /Catalog'); 
        $this
    ->_out('/Pages 1 0 R'); 
        $this
    ->_out('/OpenAction [3 0 R /FitH null]'); 
        $this
    ->_out('/PageLayout /OneColumn'); 
        $this
    ->_out('>>'); 
        $this
    ->_out('endobj'); 


    The cross reference section is very important. It brings into use the $_offset array that has appeared before. PDF stores a byte offset reference to all objects in the document. This allows the PDF reader to read objects in a random access way, without having to load the entire document.
       


    /* Print cross reference. */ 
        $start_xref 
    strlen($this->_buffer); // Get the xref offset. 
        $this->_out('xref');                  // Announce the xref. 
        $this->_out('0 ' . ($this->_n + 1));  // Number of objects. 
        $this->_out('0000000000 65535 f '); 
        /* Loop through all objects and output their offset. */ 
        for ($i = 1; $i <= $this->_n; $i++) { 
            $this->_out(sprintf('%010d 00000 n ', $this->_offsets[$i])); 
        } 


    Each object is printed on a separate line. The offset for each object is printed as a 10 digit integer, followed by a generation number, and an in-use/free indicator. You need not worry about either the generation number or the in-use/free indicator, since those will only be used if editing PDF files and deleting objects. Since we shall be generating from scratch, the generation number will always be 00000 and the in use indicator will be set to 'n'.

    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 4 hosted by Hostway