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

And Now to Output the Text - 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



You will need to pass to this method the x/y position of your text, as well as the actual text.

function text($x$y$text

$this->_escape($text);    // Escape any harmful 
                                      // characters. 
$out sprintf('BT %.2f %.2f Td (%s) Tj ET'
$this->_h $y$text); 

Note how for simplicity we allow the user to specify the y position measured from the top edge of the paper (whereas in fact PDF measures from the bottom). To achieve this we subtract the y value from the page height in the actual code ($this->_h - $y).

Also note how actual text needs to be escaped to ensure that it is safely inserted into the file. Since text in the PDF file is denoted using parentheses around it, any parentheses in the text itself should be escaped.

The best solution is to create a separate function to handle any cases when text needs to be inserted safely. This will be used a couple of times in this tutorial, but it will also be useful if you add more functionality to this class later.

function _escape($s
str_replace('\''\\'$s);   // Escape any '\' 
    $s = str_replace('(', '\(', $s);     // Escape any '(' 
    return str_replace(')', '\)', $s);   // Escape any ')' 

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