PDFlib in PHP HowTo
PDFlib in PHP HowTo
with PHP
Last change: April 15, 2008
Latest PDFlib version covered in this document: 7.0.3
Latest version of this document available at:
www.pdflib.com/developer/technical-documentation
Contact:
PDFlib GmbH
Franziska-Bilek-Weg 9
80339 München, Germany
phone +49 • 89 • 452 33 84-0
support@pdflib.com
www.pdflib.com
Notes for PDFlib TET (Text Extraction Toolkit), PDFlib PLOP (Linearization, Optimization,
Protection), and PDFlib pCOS (PDF Information Retrieval Tool) are included where appli-
cable.
The methods for deploying any of these products as a PHP extension are the same in
all cases. However, only one product of the PDFlib family can be used at a time. Similar-
ly, multiple versions of these products cannot be deployed at the same time. Different
products can coexist within one PHP installation, however. Note that the evaluation
versions of commercial PDFlib products will be fully functional, but will display a demo
stamp across all generated PDF pages unless a valid license key is applied.
The PDFlib 7.0.3 and 6.0.4 distribution packages shipped by PDFlib GmbH contain
PDFlib DSOs for a number of PHP versions. These are grouped into several directories as
follows (not all PHP versions are supported on all platforms, though):
> bind/php4/php-430 for PHP 4.3.0 – 4.3.11 and 4.4.0 – 4.4.8
> bind/php5/php-500 for PHP 5.0.0 – 5.0.2
> bind/php5/php-503 for PHP 5.0.3 – 5.0.5
> bind/php5/php-510 for PHP 5.1.0 – 5.1.6
> bind/php5/php-520 for PHP 5.2.0 – 5.2.5
Depending on the compatibility properties of the PHP distribution PDFlib may also
work with newer versions of PHP, but we have only tested the combinations above.
PHP on Mac OS X. Apple’s PHP version which is bundled with Mac OS X does not work
with PDFlib DSOs. To use PHP with PDFlib on Mac OS X you need third-party PHP pack-
ages such as MAMP, XAMP for Mac, or Marc Liyanage’s version from www.entropy.ch.
Mac OS X 10.5 (Leopard) adds new complications. As described in
developer.apple.com/releasenotes/CoreFoundation/CoreFoundation.html
it is no longer possible to use CoreFoundation functions after a call to fork( ) without
exec( ). However, CoreFoundation functions are required for PDFlib’s host font feature,
and the critical sequence above is used in the combination of Apache and PHP. This may
trigger the following error message in the Apache log (and can even crash the Apache
process):
The process has forked and you cannot use this CoreFoundation functionality safely. You
MUST exec(). Break on _THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_
FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
In order to avoid this problem you can run PHP as a CGI on Apache, or disable the host
font feature in PDFlib using the following call:
PDF_set_parameter($p, "debug", "h");
Using PDFlib Lite with PHP on platforms with DSO support. In order to use PDFlib Lite
you need PDFlib source code, and must proceed according to Section 7, »Building PDFlib
Lite from Source«. Once you’ve done this you must build a PDFlib DSO for PHP accord-
ing to Section 6, »Building a DSO from a PDFlib C library«.
Using commercial PDFlib packages with PHP on platforms without DSO support. You
need the appropriate precompiled PDFlib C library for your platform, and must rebuild
PHP according to Section 8, »Building PHP with statically linked PDFlib«.
Using PDFlib Lite with PHP on platforms without DSO support. In order to use PDFlib
Lite you need PDFlib Lite source code and must first build a PDFlib Lite C library accord-
ing to Section 7, »Building PDFlib Lite from Source«. Next, you must rebuild PHP accord-
ing to Section 8, »Building PHP with statically linked PDFlib«.
It may help to classify yourself according to these types of developers. The remainder of
this document describes tasks which require at least sysadmin or C developer skills. PHP
developers without additional knowledge or assistance will not be able to perform the
required steps without assistance.
The PHP info page. You can test the success of your PDFlib product installation and
configuration with the following mini PHP script:
<?phpinfo()?>
If you don't find a PDF section check your log files to find the reason. If the output creat-
ed by phpinfo( ) contains the line PDFlib GmbH Binary Version you are using a precompiled
PDFlib DSO provided by PDFlib GmbH. If you see a line PDFlib GmbH Version you are us-
ing your own PDFlib DSO or custom PHP with a statically linked PDFlib. The version
number of the PECL module which has been used to build the PDFlib extension will also
be shown.
Note If the output of phpinfo( ) shows only »PDFlib Version« you are using an old PDFlib extension
wrapper from a PHP 4 distribution. This combination is not supported.
The PDFlib product examples. The distribution package of your PDFlib product in-
cludes two flavors of examples which you can use to test your installation. In the bind/
php4 directory you will find the old-style examples using the functional API. These ex-
amples can be used for PHP 4 and PHP 5. In the bind/php5 directory you can find the new
object-oriented examples. These will work only with PHP 5 or above. To use the exam-
ples proceed as follows:
> Copy some php and data (if available) files to your htdocs directory:
$ cp bind/php[45]/*.php .../htdocs
$ cp bind/data/* .../htdocs/data
You are a web hosting provider. As a provider you should be aware of the following:
> Although PDFlib Lite source code is freely available, and many Linux and PHP distri-
butions contain PDFlib Lite, the PDFlib Lite license does not cover free use of PDFlib
Lite on a Web hoster’s systems.
> You can install commercial PDFlib DSOs even without obtaining a license. In this sit-
uation you can install one of the precompiled PDFlib DSOs supplied by PDFlib GmbH
without a license key (i.e. a demo stamp will be created). Those among your custom-
ers who wish to commercially use it can obtain a commercial license to disable the
demo stamp. In other words, you can offer PDFlib without the need for obtaining a li-
cense for all of your servers. The recommended method is to install the PDFlib DSO
in some globally accessible directory, and set the extension= line in php.ini appropri-
ately.
> Alternatively, if (like an increasing number of providers) you believe in PDFlib avail-
ability as a competitive advantage, you can obtain a site license which covers all your
servers and customers. Individual users will no longer be required to obtain a license
on their own in this case. Please contact PDFlib GmbH if you are interested in more
details.
Requirements:
> Skill level: sysadmin
> The PDFlib DSO, either built on your own or (preferably) from a binary package pro-
vided by PDFlib GmbH at www.pdflib.com/download/pdflib-family/pdflib-7
> Working PHP binary
This section applies to the prebuilt DSOs distributed by PDFlib GmbH, as well as to DSOs
which you have built yourself.
For the PHP installation process please follow the documentation of your PHP distribu-
tion, and copy the PDFlib DSO to the directory which is specified in the extension_dir line
in php.ini.
The Windows version of the PDFlib DSO has been built for a multithreaded version
of PHP.
Copy the PDFlib DSO to the directory which is specified in the extension_dir line in
php.ini.
The Unix versions of the PDFlib DSO have been built without multithread support.
However, libpdf_php_mt.so, which is available for some Unix platforms, has been built
for versions of PHP with experimental Zend Thread Safety (ZTS).
or
and restart your Web server so that the changes are recognized.
Loading the PDFlib DSO explicitly in your PHP script. Without the extension line in
php.ini you must include the following line in your PHP scripts:
dl("libpdf_php.dll"); (on Windows)
or
dl("libpdf_php.so"); (on Unix; adjust the shared library suffix if necessary)
The line extension_dir is not relevant in this case. Note that for security reasons this
method is no longer recommended; many Web hosters do not allow it.
Note Maintainers of Linux and PHP distributions should include PDFlib support for PHP as DSO be-
cause this facilitates updates.
Binary characteristics of PHP and PDFlib DSO must match. Several properties of your
PHP binary must match those of the PDFlib DSO. These properties are determined when
building PHP, and cannot be changed afterwards. The precompiled DSOs for PDFlib have
been built as follows:
> non-debug version
> thread-safety as described in Section 5.1, »Installing the PDFlib DSO on Windows«
and Section 5.2, »Installing the PDFlib DSO on Unix«
> the API version: choose the matching version from bind/php[45]/php-<version>
If you see an error message similar to the following when trying to load the PDFlib DSO,
your PHP build number does not match that of the PDFlib module:
Warning: pdf: Unable to initialize module
Module compiled with debug=0, thread-safety=0 module API=20020429
PHP compiled with debug=0, thread-safety=1 module API=20020429
The PDFlib extension is linked against the ApplicationServices Framework, and XAMPP
changes the DYLD_LIBRARY_PATH. This combination confuses the dynamic link editor.
We detected that commenting out DYLD_LIBRARY_PATH in xamppfiles/bin/envvars cures
this problem.
A similar problem arises with MAMP. To cure the problem with MAMP comment out
DYLD_LIBRARY_PATH in Library/bin/envvars.
and supply the path where PDFlib is installed at the following prompt:
path to pdflib installation? : $HOME/pdflib-7.0.x/bind/c
This will create a PDFlib module called pdf.so (or similar, depending on platform). After
building the extension you should check your php.ini to make sure that the extension
was added properly, and restart Apache to activate the new module.
On Unix systems (for Windows see readme-source-windows.txt in the PDFlib Lite distri-
bution) unpack the PDFlib Lite source code, change to the created directory, and issue
the following commands:
$ ./configure
$ make
$ make install
This will create a PDFlib Lite C library. For more details see readme-source-unix.txt in the
PDFlib Lite distribution.
The last step in the sequence above will install the library and include files to some
local directory. By default these files will be installed to some system directory, which
means that sysadmin permissions are required. However, you can also install to some
private (non-privileged) directory by using the --prefix option in the initial call to the
configure script, e.g.
$ ./configure --prefix=$HOME
Note that the installation will automatically create libs and include subdirectories under
the specified prefix.
If DSOs don’t work in your situation you must rebuild PHP from source code in order to
include PDFlib support. This step must be done within the PHP build process. Below you
will find an overview on how to achieve this, but you’ll have to refer the PHP documen-
tation for more details.
Building PHP. Unpack PHP, configure and build it to your needs. We recommend to
initially try this without PDFlib support in order to get accustomed with the process.
Prepare the PDFlib C library. For the commercial PDFlib product: unpack the PDFlib C
library and note the full path to the bind/c directory in this package.
For PDFlib Lite: build the PDFlib Lite C library according to Section 7, »Building PDFlib
Lite from Source«.
Prepare the PECL Package. Create an ext/pdf directory in your PHP source tree. Unpack
the PECL package for PDFlib, and move the contents of this package (including pdf.c,
php_pdf.h and others) to the ext/pdf directory of your PHP source tree.
Put everything together. Change to the PHP source directory and follow these steps:
# first remove the configure script so that buildconf is forced to create it
$ rm ./configure
$ make
....
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).
# install it as root
# probably you have to configure your Web server to load the new php module
Notes for Mac OS X. As mentioned in Section 2.1, »Platforms where PHP reliably sup-
ports DSOs« only some combinations of PHP versions and versions of Mac OS X reliably
support DSOs. Other combination may require some manual tweaking where the fol-
lowing notes may be useful:
> On Mac OS X pear install will fail if no libtoolize is available. Either install it from the
Apple Developer Tools CD, or try to change libtoolize to glibtoolize in the phpize script
on your system.
> If the configure script fails on Mac OS X you must set the LDFLAGS environment vari-
able appropriately. Use the following commands in the C shell (tcsh or csh):
$ setenv LDFLAGS "-framework ApplicationServices"