You can use the PDF functions in PHP to create PDF files if you have the PDF library by Thomas Merz (available at http://www.pdflib.com/pdflib/index.html; you will also need the JPEG library and the TIFF library to compile this. These two librariess can often cause problems when configuring PHP, so be sure to pay attention to the output of the configure script and fix any problems it reports.
Please consult the documentation for PDFlib shipped with the source distribution of PDFlib. It provides a very good overview of what PDFlib is capable of doing and contains the full and most up-to-date description of all functions.
All of the functions in PDFlib and the PHP module have the same name. The parameters are also identical. You should also understand some of the concepts of PDF or PostScript to efficiently use this module. 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.
There is another PHP module for PDF document creation based on FastIO's. ClibPDF. It has a slightly different API. Check the ClibPDF functions section for details.
The PDF module introduces one new type of variable. It is called a pdf object and almost all functions need a pdf object as its first parameter.
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-manual (V3.00 or greater) are supported by PHP 4 with exactly the same meaning and the same parameters. Only the return values may differ from the PDFlib manual, because the PHP convention of returning false was adopted. For compatibility reasons this binding for PDFlib still supports the old functions, but as stated above they should be replaced by their new versions. PDFlib GmbH will not support any problems arising from the use of these deprecated functions.
Table 1. Deprecated functions and its replacements
Old function | Replacement |
---|---|
PDF_put_image() | Not needed anymore. |
PDF_execute_image() | Not needed anymore. |
PDF_get_annotation() | PDF_get_bookmark() using the same parameters. |
PDF_get_font() | PDF_get_value() passing "font" as the second parameter. |
PDF_get_fontsize() | PDF_get_value() passing "fontsize" as the second parameter. |
PDF_get_fontname() | PDF_get_parameter() passing "fontname" as the second parameter. |
PDF_set_info_creator() | PDF_set_info() passing "Creator" as the second parameter. |
PDF_set_info_title() | PDF_set_info() passing "Title" as the second parameter. |
PDF_set_info_subject() | PDF_set_info() passing "Subject" as the second parameter. |
PDF_set_info_author() | PDF_set_info() passing "Author" as the second parameter. |
PDF_set_info_keywords() | PDF_set_info() passing "Keywords" as the second parameter. |
PDF_set_leading() | PDF_set_value() passing "leading" as the second parameter. |
PDF_set_text_rendering() | PDF_set_value() passing "textrendering" as the second parameter. |
PDF_set_text_rise() | PDF_set_value() passing "textrise" as the second parameter. |
PDF_set_horiz_scaling() | PDF_set_value() passing "horizscaling" as the second parameter. |
PDF_set_text_matrix() | Not available anymore |
PDF_set_char_spacing() | PDF_set_value() passing "charspacing" as the second parameter. |
PDF_set_word_spacing() | PDF_set_value() passing "wordspacing" as the second parameter. |
PDF_set_transition() | PDF_set_parameter() passing "transition" as the second parameter. |
PDF_open() | PDF_new() plus an subsequent call of PDF_open_file() |
PDF_set_font() | PDF_findfont() plus an subsequent call of PDF_setfont() |
PDF_set_duration() | PDF_set_value() passing "duration" as the second parameter. |
PDF_open_gif() | PDF_open_image_file() passing "gif" as the second parameter. |
PDF_open_jpeg() | PDF_open_image_file() passing "jpeg" as the second parameter. |
PDF_open_tiff() | PDF_open_image_file() passing "tiff" as the second parameter. |
PDF_open_png() | PDF_open_image_file() passing "png" as the second parameter. |
PDF_get_image_width() | PDF_get_value() passing "imagewidth" as the second parameter and the image as the third parameter. |
PDF_get_image_height() | PDF_get_value() passing "imageheight" as the second parameter and the image as the third parameter. |
() | () |
Since version 3.0 of PDFlib you should configure PDFlib with the option --enable-shared-pdflib.
Any version of PHP 4 after March 9, 2000 does not support versions of PDFlib older than 3.0.
PDFlib 3.0 or greater is also supported by PHP 3.0.19 and later.
Most of the functions are fairly easy to use. The most difficult part is probably creating a very simple PDF document at all. The following example should help to get started. It creates test.pdf with one page. The page contains the text "Times Roman outlined" in an outlined, 30pt font. The text is also underlined.
The PDFlib distribution contains a more complex example which creates a page with an analog clock. Here we use the in memory creation feature of PDFlib, so we don't need any tmp-files. This example converted into PHP using PDFlib looks as the following (you can see the same example in the documentation for the clibpdf module):
Example 2. pdfclock example from PDFlib distribution
|