Zend
  Home arrow Zend arrow Page 5 - 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

    Lose your application development headaches. Start developing and deploying applications with Advantage Database Server today. Download a 30-day trial for Free!

    PDFs with PHP part 1 - Adding a Page
    (Page 5 of 10 )


    We can now add a page to our document. The following code is quite straightforward.

    One point worth noting is the $_font_family check. For any text to be written to a page we need to set the font. However, we have to take into account the possibility that the font was set before any page was added, or that the font was set for a previous page in the current document. Either way we need to check the font class variable, and output the font information to the page. The function setFont() is used for this, which we shall cover later.


    function addPage() 
    {    
        $this
    ->_page++;                   // Increment page count. 
        $this->_pages[$this->_page] = ''; // Start the page buffer. 
        $this->_state = 2;                // Set state to page 
                                          // opened. 
        /* Check if font has been set before this page. */ 
        if ($this->_font_family) { 
            $this->setFont($this->_font_family, $this->_font_style, $this->_font_size); 
        } 




    Output of Simple Text
    As mentioned earlier, before any text can be output, font information must be supplied. We therefore need a function to define which font will be used. PDF specifications offer a core set of fonts which can be used with no extra information supplied to the PDF reader. You can also embed your own custom fonts into a PDF file, but for this you need to create font definitions, which are beyond the scope of this tutorial.

    For now, limit your output to the following fonts:

    • Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique;
    • Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique;
    • Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic;
    • Symbol;
    • ZapfDingbats.

    The following method sets the font family name, and also (optionally) a style such as bold, italic or both, and a font size.

     
    function setFont($family$style ''$size null

        $family 
    strtolower($family); 
        
    if ($family == 'arial') {               // Use helvetica. 
            $family = 'helvetica'; 
        } elseif ($family == 'symbol' ||        // No styles for 
                  $family == 'zapfdingbats') {  // these two fonts. 
            $style = ''; 
        } 
        
    $style strtoupper($style); 
        
    if ($style == 'IB') {                   // Accept any order 
            $style = 'BI';                      // of B and I. 
        } 
        
    if (is_null($size)) {                   // No size specified, 
            $size = $this->_font_size;          // use current size. 
        } 
        
    if ($this->_font_family == $family &&   // If font is already 
            $this->_font_style == $style &&     // current font 
            $this->_font_size == $size) {       // simply return. 
            return; 
        } 
        /* Set the font key. */ 

        $fontkey 
    $family $style
        if (!isset(
    $this->_fonts[$fontkey])) {  // Test if cached. 
            $i = count($this->_fonts) + 1;      // Increment font 
            $this->_fonts[$fontkey] = array(    // object count and 
                'i'    => $i,                   // store cache. 
                'name' => $this->_core_fonts[$fontkey]); 
        } 
        /* Store current font information. */ 

        $this
    ->_font_family  $family
        $this
    ->_font_style   $style
        $this
    ->_font_size    $size
        $this
    ->_current_font $this->_fonts[$fontkey]; 
        
    /* Output font information if at least one page has been 
         * defined. */ 

        
    if ($this->_page 0) { 
            $this
    ->_out(sprintf('BT /F%d %.2f Tf ET'$this->_current_font['i'], $this->_font_size)); 
        




    The following method enables easier changing between font sizes, without having to go through the whole setFont() function.


    function setFontSize($size

        
    if ($this->_font_size == $size) {   // If already current 
            return;                         // size simply return. 
        } 
        
    $this->_font_size $size;          // Set the font. 
        /* Output font information if at least one page has been 
         * defined. */ 

        
    if ($this->_page 0) { 
            $this
    ->_out(sprintf('BT /F%d %.2f Tf ET'
                                $this
    ->_current_font['i'], 
                                $this
    ->_font_size)); 
        




    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