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

The Factory Method - 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

This method will give us the PDF object with which we can build our document. It sets the initial values for the document, such as page orientation and size, and returns the object.

 
function &factory($orientation 'P'$format 'A4'

    
/* Create the PDF object. */ 
    $pdf 
= &new PDF(); 
    
/* Page format. */ 
    $format 
strtolower($format); 
    
if ($format == 'a3') {           // A3 page size. 
        $format = array(841.89, 1190.55); 
    } elseif ($format == 'a4') {     // A4 page size. 
        $format = array(595.28, 841.89); 
    } elseif ($format == 'a5') {     // A5 page size. 
        $format = array(420.94, 595.28); 
    } elseif ($format == 'letter') { // Letter page size. 
        $format = array(612, 792); 
    } elseif ($format == 'legal') {  // Legal page size. 
        $format = array(612, 1008); 
    } else { 
        die(sprintf('Unknown page format: %s', $format)); 
    }    
    $pdf->_w = $format[0]; 
    $pdf->_h = $format[1]; 
    /* Page orientation. */ 

    $orientation 
strtolower($orientation); 
    
if ($orientation == 'l' || $orientation == 'landscape') { 
        $w 
$pdf->_w
        $pdf
->_w $pdf->_h
        $pdf
->_h $w
    
} elseif ($orientation != 'p' && $orientation != 'portrait') { 
        
die(sprintf('Incorrect orientation: %s'$orientation)); 
    

    
/* Turn on compression by default. */ 
    $pdf
->setCompression(true); 
    return 
$pdf



Also in this method we turn on compression by default. This makes the output PDF files a lot smaller.

The actual setCompression() method is as follows:


function setCompression($compress
{    
    
/* If no gzcompress function is available then default to 
     * false. */ 

    $this
->_compress = (function_exists('gzcompress') ? $compress false); 



However, whilst learning you may wish to explicitly turn off compression, so that you can open your created PDF document with a text editor and see easily what is happening.



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