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 libs also quite often make problems when configuring php. Follow the messages of configure to fix possible problems.
Please consult the excellent documentation for PDFlib shipped with the source distribution of PDFlib. It provides a very good overview of what PDFlib capable of doing and contains the full and uptodate 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 pdf object and almost all functions need pdf object as its first parameter.
Starting with php V4.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 php4 with exactly the same meaning and the same parameters. Only the returnvalues may differ from the PDFlib manual, as we adoptet the PHP way to return FALSE in case of errors. For compatibility reasons this binding for PDFlib still will support the old functions, but as stated above they should be replaced by their new versions. PDFlib GmbH will not support any problems arraising from the use of these depreciated 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, 9th 2000 do not support versions of PDFlib older than 3.0.
PHP 3 on the other hand should not be used with version newer than 2.01. Since revision 1.61 of php3/functions/pdf.c (php 3.19) it is save to use PDFlib 3.0 or greater.
Most of the functions are fairly easy to use. The most difficult part is probably to create a very simple pdf document at all. The following example should help to get started. It creates the file 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
|