The PDF functions in PHP can create PDF files using the PDFlib
library created by Thomas Merz.
The documentation in this section is only meant to be an overview
of the available functions in the PDFlib library and should not be
considered an exhaustive reference. For the full and detailed
explanation of each function, consult the PDFlib Reference Manual
which is included in all PDFlib packages distributed by PDFlib GmbH.
It provides a very good overview of what PDFlib is capable of doing
and contains the most up-to-date documentation of all functions.
All of the functions in PDFlib and the PHP module have identical
function names and parameters. You will need to understand some
of the basic concepts of PDF and PostScript to efficiently use
this extension. All lengths and coordinates are measured in
PostScript points. There are generally 72 PostScript points to an
inch, but this depends on the output resolution. Please see the
PDFlib Reference Manual included in the PDFlib distribution for a more
thorough explanation of the coordinate system used.
With version 6, PDFlib offers an object oriented API for PHP 5 in
addition to the function oriented API for PHP 4. The main difference is
the following: In PHP 4, first a PDF resource has to be retrieved
with a function call like $p = PDF_new();()
which is used as the first parameter in all further function calls, e.g.
as in PDF_begin_document($p, "", "")().
In PHP 5, a PDFlib object is created with
$p = new PDFlib()() instead. This object offers
all PDFlib API functions as methods, e.g. as with
$p->begin_document("", "")().
In addition, exceptions have been introduced in PHP 5 which are
supported by PDFlib 6 and later as well.
Please see the examples below for
more information.
Note:
If you're interested in alternative free PDF generators that do not
utilize external PDF libraries, see
this related FAQ.
Note:
This extension has been moved to PECL as
of PHP 4.3.9.
This PECL extension
is not bundled with PHP.
Additional information such as new releases,
downloads, source files, maintainer information, and a CHANGELOG, can be
located here:
http://pecl.php.net/package/pdflib.
To get these functions to work in PHP < 4.3.9, you have to compile PHP with
--with-pdflib[=DIR]. DIR is the PDFlib
base install directory, defaults to /usr/local.
As of PHP 4.3.9, you must install this extension through PEAR, using the following command:
pear install pdflib.
Starting with PHP 4.0.5, the PHP extension for PDFlib is
officially supported by PDFlib GmbH. This means that all the
functions described in the PDFlib Reference Manual (PDFlib V3.0 or higher) are
supported by PHP 4 with exactly the same meaning and the same
parameters. However, with PDFlib V5.0.4 or higher all parameters
have to be specified. For compatibility reasons,
this binding for PDFlib still supports most of the deprecated functions, but they
should be replaced by their new versions. PDFlib GmbH will not
support any problems arising from the use of these deprecated
functions. The documentation in this section indicates old functions as
"Deprecated" and gives the replacement function to be used instead.
Most of the functions are fairly easy to use. The most difficult part
is probably creating your first PDF document. The following
example should help to get you started. It is developed for PHP 4 and
creates the file hello.pdf with one page.
It defines some document info field contents, loads the Helvetica-Bold
font and outputs the text "Hello world! (says PHP)".
Example 1. Hello World example from PDFlib distribution for PHP 4
<?php $p = PDF_new();
/* open new PDF file; insert a file name to create the PDF on disk */ if (PDF_begin_document($p, "", "") == 0) { die("Error: " . PDF_get_errmsg($p)); }
The following example comes with the PDFlib distribution for PHP 5.
It uses the new exception handling and object encapsulation features
available in PHP 5. It creates the file hello.pdf with one page.
It defines some document info field contents, loads the Helvetica-Bold
font and outputs the text "Hello world! (says PHP)".
Example 2. Hello World example from PDFlib distribution for PHP 5
<?php
try { $p = new PDFlib();
/* open new PDF file; insert a file name to create the PDF on disk */ if ($p->begin_document("", "") == 0) { die("Error: " . $p->get_errmsg()); }
$p->set_info("Creator", "hello.php"); $p->set_info("Author", "Rainer Schaaf"); $p->set_info("Title", "Hello world (PHP)!");