Zend
  Home arrow Zend arrow Page 10 - PDFs with PHP part 1
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Forums Sitemap 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
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? 
Google.com  
ZEND

PDFs with PHP part 1
By: Zend
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    PDFs with PHP part 1 - Resources
    (Page 10 of 10 )


    Whilst the content streams define the objects on a page, sometimes they need to reference objects outside the content stream. These are called resources. Resources are named objects such as font information or image data. The following method includes any resources defined so far into the main buffer. In this first part of the tutorial fonts are the only resources we will be dealing with.


    function _putResources() 

        
    /* Output any fonts. */ 
        $this
    ->_putFonts(); 
        
    /* Resources are always object number 2. */ 
        $this
    ->_offsets[2] = strlen($this->_buffer); 
        $this
    ->_out('2 0 obj'); 
        $this
    ->_out('<</ProcSet [/PDF /Text]'); 
        $this
    ->_out('/Font <<'); 
        
    foreach ($this->_fonts as $font) { 
            $this
    ->_out('/F' $font['i'] . ' ' $font['n'] . ' 0 R'); 
        

        $this
    ->_out('>>'); 
        $this
    ->_out('>>'); 
        $this
    ->_out('endobj'); 



    This last private function, called in the above _putResources() method, includes any font names into the PDF file. As we are only covering core fonts in this tutorial, nothing more than listing the font names is done here.


    function _putFonts() 

        
    /* Print out font details. */ 
        
    foreach ($this->_fonts as $k => $font) { 
            $this
    ->_newobj(); 
            $this
    ->_fonts[$k]['n'] = $this->_n
            $name 
    $font['name']; 
            $this
    ->_out('<</Type /Font'); 
            $this
    ->_out('/BaseFont /' $name); 
            $this
    ->_out('/Subtype /Type1'); 
            
    if ($name != 'Symbol' && $name != 'ZapfDingbats') { 
                $this
    ->_out('/Encoding /WinAnsiEncoding'); 
            

            $this
    ->_out('>>'); 
            $this
    ->_out('endobj'); 
        




    Document Output
    The following function, the actual output of the document, does nothing more than make sure the document is closed, send a few headers according to browser type, and echo the buffered data.


    function output($filename

        
    if ($this->_state 3) {    // If document not yet closed 
            $this->close();         // close it now. 
        } 
        /* Make sure no content already sent. */ 

        
    if (headers_sent()) { 
            
    die('Unable to send PDF file, some data has already been output to browser.'); 
        

        
    /* Offer file for download and do some browser checks 
         * for correct download. */ 

        $agent 
    trim($_SERVER['HTTP_USER_AGENT']); 
        
    if ((preg_match('|MSIE ([0-9.]+)|'$agent$version)) || 
            
    (preg_match('|Internet Explorer/([0-9.]+)|'$agent$version))) { 
            header
    ('Content-Type: application/x-msdownload'); 
            Header
    ('Content-Length: ' strlen($this->_buffer)); 
            
    if ($version == '5.5') { 
                header
    ('Content-Disposition: filename="' $filename '"'); 
            
    } else { 
                header
    ('Content-Disposition: attachment; filename="' $filename '"'); 
            

        
    } else { 
            Header
    ('Content-Type: application/pdf'); 
            Header
    ('Content-Length: ' strlen($this->_buffer)); 
            Header
    ('Content-disposition: attachment; filename=' $filename); 
        

        
    echo $this->_buffer




    The Script
    The complete class
    You can download the entire class for use with Part 1 of this tutorial.
    Example Use


    <?php 
    require 
    'PDF.php';                         // Require the lib. 
    $pdf = &PDF::factory('p', 'a4');       // Set up the pdf object. 
    $pdf->open();                             // Start the document. 
    $pdf->setCompression(true);         // Activate compression. 
    $pdf->addPage();                        // Start a page. 
    $pdf->setFont('Courier', '', 8);        // Set font to arial 8 pt. 
    $pdf->text(100, 100, 'First page');  // Text at x=100 and y=100. 
    $pdf->setFontSize(20);                 // Set font size to 20 pt. 
    $pdf->text(100, 200, 'HELLO WORLD!'); // Text at x=100 and y=200. 
    $pdf->addPage();                         // Add a new page. 
    $pdf->setFont('Arial', 'BI', 12);        // Set font to arial bold italic 12 pt. $pdf->text(100, 100, 'Second page');  // Text at x=100 and y=200. 
    $pdf->output('foo.pdf');              // Output the file named foo.pdf 
    ? >

     


     



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





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek