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

Compression - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If compression is required page content will be passed through the gzcompress() function before being written to output. Here you also can see why the $_n object counter starts from 2. We set the root pages parent as object number 1, and later you will see that we set resources as object number 2. This is just so that it is easier for us to reference these when required, for example in each page object.


function _putPages() 

    
/* If compression is required set the compression tag. */ 
    $filter 
= ($this->_compress) ? '/Filter /FlateDecode ' ''
    
/* Print out pages, loop through each. */ 
    
for ($n 1$n <= $this->_page$n++) { 
        $this
->_newobj();                 // Start a new object. 
        $this->_out('<</Type /Page');     // Object type. 
        $this->_out('/Parent 1 0 R'); 
        $this->_out('/Resources 2 0 R'); 
        $this->_out('/Contents ' . ($this->_n + 1) . ' 0 R>>'); 
        $this->_out('endobj'); 
        /* If compression required gzcompress() the page content. */ 

        $p 
= ($this->_compress) ? gzcompress($this->_pages[$n]) : $this->_pages[$n]; 
        
/* Output the page content. */ 
        $this
->_newobj();                 // Start a new object. 
        $this->_out('<<' . $filter . '/Length ' . strlen($p) . '>>'); 
        $this->_putStream($p);            // Output the page. 
        $this->_out('endobj'); 
    } 
    /* Set the offset of the first object. */ 

    $this
->_offsets[1] = strlen($this->_buffer); 
    $this
->_out('1 0 obj'); 
    $this
->_out('<</Type /Pages'); 
    $kids 
'/Kids ['
    
for ($i 0$i $this->_page$i++) { 
        $kids 
.= ($i) . ' 0 R '
    
}    
    $this
->_out($kids ']'); 
    $this
->_out('/Count ' $this->_page); 
    
/* Output the page size. */ 
    $this
->_out(sprintf('/MediaBox [0 0 %.2f %.2f]'
                        $this
->_w$this->_h)); 
    $this
->_out('>>'); 
    $this
->_out('endobj'); 




Letís look at another method now: _putStream(). We could have included the code in the actual _putPages() function, however, since this method is required for other objects (such as images), we might as well separate it out now.


function _putStream($s

    $this
->_out('stream'); 
    $this
->_out($s); 
    $this
->_out('endstream'); 






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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: