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.

  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



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


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) { 
->_fonts[$k]['n'] = $this->_n
->_out('<</Type /Font'); 
->_out('/BaseFont /' $name); 
->_out('/Subtype /Type1'); 
if ($name != 'Symbol' && $name != 'ZapfDingbats') { 
->_out('/Encoding /WinAnsiEncoding'); 


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. */ 

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

} else { 
('Content-Type: application/pdf'); 
('Content-Length: ' strlen($this->_buffer)); 
('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

'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


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