Russian Ukraine English
Catalog RSS


LI. PDF functions

Introduction

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. Most 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 two new type of variable. It is called pdfdoc pdfdoc is a pointer to a pdf document and almost all functions need it as its first parameter.

Confusion with old pdflib versions

Since the very begining of PDF support in PHP — starting with pdflib 0.6 — there has been tons of changes especially to the pdflib API. Most of these changes has been somehow covered by PHP, some has even required changes to the PHP API. Since pdflib 3.x the API seems to be stabilzed and PHP 4 has adopted the version as a minimum requirement for PDF support. The consequence will be that many functions will disappear or be replaced by alternatives sooner or later. Support for pdflib 0.6 is already completely given up. The following table list all the functions which are deprecated in PHP 4.02 and should be replaced by their new versions.

Table 1. Deprecated functions and its replacements

Old functionReplacement
pdf_put_image()Not needed anymore.
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_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_imagewidth()pdf_get_value() passing "imagewidth" as the second parameter and the image as the third parameter.
pdf_get_imageheight()pdf_get_value() passing "imageheight" as the second parameter and the image as the third parameter.
()()

Hints for installation of pdflib 3.x

Since version 3.0 of pdflib you should configure pdflib with the option --enable-shared-pdflib.

Issues with older versions of pdflib

If you use pdflib 2.01 check how the lib was installed. There should be a file or a to link libpdf.so. Version 2.01 just creates a lib with the name libpdf2.01.so which cannot be found when linking the test programm in configure. You will have to create a symbolic link from libpdf.so to libpdf2.01.so.).

Version 2.20 of pdflib has introduced more changes to its API and support for chinese and japanese fonts. This unfortunately causes some changes of the pdf module of php4 (not php3). If you use pdflib 2.20 handle the in memory generation of PDF documents with care. Until pdflib 3.0 is released it might be unstable. The encoding parameter of pdf_set_font() has changed to a string. This means that instead of e.g. 4 you have to use 'winansi'.

If you use pdflib 2.30 the pdf_set_text_matrix() will have gone. It is not supported any more. In general it is a good advise to consult the release notes of the used version of pdflib for possible changes.

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.

Examples

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.

Example 1. Creating a PDF document with pdflib


<?php $fp = fopen("test.pdf", "w"); $pdf = pdf_open($fp); pdf_set_info($pdf, "Author", "Uwe Steinmann"); pdf_set_info($pdf, "Title", "Test for PHP wrapper of PDFlib 2.0"); pdf_set_info($pdf, "Creator", "See Author"); pdf_set_info($pdf, "Subject", "Testing"); pdf_begin_page($pdf, 595, 842); pdf_add_outline($pdf, "Page 1"); pdf_set_font($pdf, "Times-Roman", 30, "host"); pdf_set_value($pdf, "textrendering", 1); pdf_show_xy($pdf, "Times Roman outlined", 50, 750); pdf_moveto($pdf, 50, 740); pdf_lineto($pdf, 330, 740); pdf_stroke($pdf); pdf_end_page($pdf); pdf_close($pdf); fclose($fp); echo "<A HREF=getpdf.php>finished</A>"; ?>       

The script getpdf.php just returns the pdf document.


<?php $fp = fopen("test.pdf", "r"); header("Content-type: application/pdf"); fpassthru($fp); fclose($fp); ?>        

The pdflib distribution contains a more complex example which creates a serious of pages with an analog clock. 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


<?php $pdffilename = "clock.pdf"; $radius = 200; $margin = 20; $pagecount = 40;  $fp = fopen($pdffilename, "w"); $pdf = pdf_open($fp); pdf_set_info($pdf, "Creator", "pdf_clock.php3"); pdf_set_info($pdf, "Author", "Uwe Steinmann"); pdf_set_info($pdf, "Title", "Analog Clock");  while($pagecount-- > 0) {     pdf_begin_page($pdf, 2 * ($radius + $margin), 2 * ($radius + $margin));      pdf_set_parameter($pdf, "transition", "wipe");     pdf_set_value($pdf, "duration", 0.5);        pdf_translate($pdf, $radius + $margin, $radius + $margin);     pdf_save($pdf);     pdf_setrgbcolor($pdf, 0.0, 0.0, 1.0);      /* minute strokes */     pdf_setlinewidth($pdf, 2.0);     for ($alpha = 0; $alpha < 360; $alpha += 6) {         pdf_rotate($pdf, 6.0);         pdf_moveto($pdf, $radius, 0.0);         pdf_lineto($pdf, $radius-$margin/3, 0.0);         pdf_stroke($pdf);     }      pdf_restore($pdf);     pdf_save($pdf);      /* 5 minute strokes */     pdf_setlinewidth($pdf, 3.0);     for ($alpha = 0; $alpha < 360; $alpha += 30) {          pdf_rotate($pdf, 30.0);         pdf_moveto($pdf, $radius, 0.0);         pdf_lineto($pdf, $radius-$margin, 0.0);         pdf_stroke($pdf);     }      $ltime = getdate();      /* draw hour hand */     pdf_save($pdf);     pdf_rotate($pdf,-(($ltime['minutes']/60.0)+$ltime['hours']-3.0)*30.0);     pdf_moveto($pdf, -$radius/10, -$radius/20);     pdf_lineto($pdf, $radius/2, 0.0);     pdf_lineto($pdf, -$radius/10, $radius/20);     pdf_closepath($pdf);     pdf_fill($pdf);     pdf_restore($pdf);      /* draw minute hand */     pdf_save($pdf);     pdf_rotate($pdf,-(($ltime['seconds']/60.0)+$ltime['minutes']-15.0)*6.0);     pdf_moveto($pdf, -$radius/10, -$radius/20);     pdf_lineto($pdf, $radius * 0.8, 0.0);     pdf_lineto($pdf, -$radius/10, $radius/20);     pdf_closepath($pdf);     pdf_fill($pdf);     pdf_restore($pdf);      /* draw second hand */     pdf_setrgbcolor($pdf, 1.0, 0.0, 0.0);     pdf_setlinewidth($pdf, 2);     pdf_save($pdf);     pdf_rotate($pdf, -(($ltime['seconds'] - 15.0) * 6.0));     pdf_moveto($pdf, -$radius/5, 0.0);     pdf_lineto($pdf, $radius, 0.0);     pdf_stroke($pdf);     pdf_restore($pdf);      /* draw little circle at center */     pdf_circle($pdf, 0, 0, $radius/30);     pdf_fill($pdf);      pdf_restore($pdf);      pdf_end_page($pdf); }  $pdf = pdf_close($pdf); fclose($fp); echo "<A HREF=getpdf.php?filename=".$pdffilename.">finished</A>"; ?>       

The PHP script getpdf.php just outputs the pdf document.


<?php $fp = fopen($filename, "r"); header("Content-type: application/pdf"); fpassthru($fp); fclose($fp); ?>       

Table of Contents
pdf_set_info — Fills a field of the document information
pdf_open — Opens a new pdf document
pdf_close — Closes a pdf document
pdf_begin_page — Starts new page
pdf_end_page — Ends a page
pdf_show — Output text at current position
pdf_show_boxed — Output text in a box
pdf_show_xy — Output text at given position
pdf_set_font — Selects a font face and size
pdf_set_leading — Sets distance between text lines
pdf_set_parameter — Sets certain parameters
pdf_get_parameter — Gets certain parameters
pdf_set_value — Sets certain numerical value
pdf_get_value — Gets certain numerical value
pdf_get_image_height — Returns height of an image
pdf_get_image_width — Returns width of an image
pdf_set_text_rendering — Determines how text is rendered
pdf_set_horiz_scaling — Sets horizontal scaling of text
pdf_set_text_rise — Sets the text rise
pdf_set_text_matrix — Sets the text matrix
pdf_set_text_pos — Sets text position
pdf_set_char_spacing — Sets character spacing
pdf_set_word_spacing — Sets spacing between words
pdf_skew — Skews the coordinate system
pdf_continue_text — Outputs text in next line
pdf_stringwidth — Returns width of text using current font
pdf_save — Saves the current environment
pdf_restore — Restores formerly saved environment
pdf_translate — Sets origin of coordinate system
pdf_scale — Sets scaling
pdf_rotate — Sets rotation
pdf_setflat — Sets flatness
pdf_setlinejoin — Sets linejoin parameter
pdf_setlinecap — Sets linecap parameter
pdf_setmiterlimit — Sets miter limit
pdf_setlinewidth — Sets line width
pdf_setdash — Sets dash pattern
pdf_moveto — Sets current point
pdf_curveto — Draws a curve
pdf_lineto — Draws a line
pdf_circle — Draws a circle
pdf_arc — Draws an arc
pdf_rect — Draws a rectangle
pdf_closepath — Closes path
pdf_stroke — Draws line along path
pdf_closepath_stroke — Closes path and draws line along path
pdf_fill — Fills current path
pdf_fill_stroke — Fills and strokes current path
pdf_closepath_fill_stroke — Closes, fills and strokes current path
pdf_endpath — Ends current path
pdf_clip — Clips to current path
pdf_setgray_fill — Sets filling color to gray value
pdf_setgray_stroke — Sets drawing color to gray value
pdf_setgray — Sets drawing and filling color to gray value
pdf_setrgbcolor_fill — Sets filling color to rgb color value
pdf_setrgbcolor_stroke — Sets drawing color to rgb color value
pdf_setrgbcolor — Sets drawing and filling color to rgb color value
pdf_add_outline — Adds bookmark for current page
pdf_set_transition — Sets transition between pages
pdf_set_duration — Sets duration between pages
pdf_open_gif — Opens a GIF image
pdf_open_png — Opens a PNG image
pdf_open_image_file — Reads an image from a file
pdf_open_memory_image — Opens an image created with PHP's image functions
pdf_open_jpeg — Opens a JPEG image
pdf_open_tiff — Opens a TIFF image
pdf_close_image — Closes an image
pdf_place_image — Places an image on the page
pdf_put_image — Stores an image in the PDF for later use
pdf_execute_image — Places a stored image on the page
pdf_add_annotation — Adds annotation
pdf_set_border_style — Sets style of border around links and annotations
pdf_set_border_color — Sets color of border around links and annotations
pdf_set_border_dash — Sets dash style of border around links and annotations
Led