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

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

You need to have a fully functional PHP install (either PHP 4 or PHP 5 will work here) and a running web server to output the PDF file from your script.

Acrobat Reader, XPDF, or an equivalent is required to see the results of your work.

You do not need any external library, either separate or compiled into PHP, to generate your PDF files.

How It Works

The best approach is to set the code up as a class. This allows for greater flexibility later.

The primary (public) methods deal with the main operations on a PDF document: setting it up, adding pages, setting font, adding text, activating compression, and output of the document.

We shall review the various methods and features of the PDF language, and then eventually put it all together as one class.

Setting up Class Variables
We will need a few class variables to keep track of output, pages, objects, settings, etc.

The following is a list of the essential variables, with brief comments. You will later see each one of these variables in its context, which will give you a better idea of how they are used. For now just briefly get yourself acquainted with them.
var $_buffer = '';          // Buffer holding in-memory PDF. var $_state = 0;            // Current document state.
var $_page = 0;             // Current page number.
var $_n = 2;                // Current object number.
var $_offsets = array();    // Array of object offsets.
var $_pages = array();      // Array containing the pages.
var $_w;                    // Page width in points.
var $_h;                    // Page height in points
var $_fonts = array();      // An array of used fonts.
var $_font_family = '';     // Current font family.
var $_font_style = '';      // Current font style.
var $_current_font;         // Array with current font info. var $_font_size = 12;       // Current font size in points.
var $_compress;             // Flag to compress or not.
var $_core_fonts = array('courier'=> 'Courier',
                         'courierB'=> 'Courier-Bold',
                         'courierI'=> 'Courier-Oblique',
                         'courierBI'=> 'Courier-BoldOblique',
                         'helvetica'=> 'Helvetica',
                         'helveticaB'=> 'Helvetica-Bold',
                         'helveticaI'=> 'Helvetica-Oblique',
                         'helveticaBI'=> 'Helvetica-BoldOblique',
                         'times'=> 'Times-Roman',
                         'timesB'=> 'Times-Bold',
                         'timesI'=> 'Times-Italic',
                         'timesBI'=> 'Times-BoldItalic',
                         'symbol'=> 'Symbol',
                         'zapfdingbats' => 'ZapfDingbats');



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