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

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

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
 

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: