PHP Manual
Prev |  | Next

# XXXIX. PDF functions

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. If you use pdflib 2.01 check how the lib was installed. There should be file or 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.

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.

Currently all versions of pdflib are supported. It is recommended that you use the newest version since it has more features and fixes some problems which required a patch for the old version. Unfortunately, the changes of the pdflib API in 2.x compared to 0.6 have been so severe that even some PHP functions had to be altered. Here is a list of changes:

- The Info structure does not exist anymore. Therefore the function pdf_get_info() is obsolete and the functions pdf_set_info_creator(), pdf_set_info_title(), pdf_set_info_author(), pdf_set_info_subject() and pdf_set_info_keywords() do not take the info structure as the first parameter but the pdf document. This also means that the pdf document must be opened before these functions can be called.

- The way a new document is opened has changed. The function pdf_open() takes only one parameter which is the file handle of a file opened with fopen().

There were some more changes with the release 2.01 of pdflib which should be covered by PHP. Some functions are not required anymore (e.g. pdf_put_image()). You will get a warning so don't be shocked.

The pdf module introduces two new types of variables (if pdflib 2.x is used it is only one new type). They are called `pdfdoc` and `pdfinfo` (`pdfinfo` is not existent if pdflib 2.x is used. `pdfdoc` is a pointer to a pdf document and almost all functions need it as its first parameter. `pdfinfo` contains meta data about the PDF document. It has to be set before pdf_open() is called.

> Note: The following is only true for pdflib 0.6. Read the pdflib manual for newer version

In order to output text into a PDF document you will need to provide the afm file for each font. Afm files contain font metrics for a Postscript font. By default these afm files are searched for in a directory named 'fonts' relative to the directory where the PHP script is located. (Again, this was true for pdflib 0.6, newer versions do not not neccessarily need the afm files.)

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 uses the PHP functions for pdflib 0.6. It creates the file test.pdf with one page. The page contains the text "Times-Roman" in an outlined 30pt font. The text is also underlined.

Example 1. Creating a PDF document with pdflib 0.6
      1
      2 <?php
      3 $fp = fopen("test.pdf", "w");
      4 $info = PDF_get_info();
      5 pdf_set_info_author($info, "Uwe Steinmann");
      6 PDF_set_info_title($info, "Test for PHP wrapper of PDFlib 0.6");
      7 PDF_set_info_author($info, "Name of Author");
      8 pdf_set_info_creator($info, "See Author");
      9 pdf_set_info_subject($info, "Testing");
     10 $pdf = PDF_open($fp, $info);
     11 PDF_begin_page($pdf, 595, 842);
     12 PDF_add_outline($pdf, "Page 1");
     13 pdf_set_font($pdf, "Times-Roman", 30, 4);
     14 pdf_set_text_rendering($pdf, 1);
     15 PDF_show_xy($pdf, "Times Roman outlined", 50, 750);
     16 pdf_moveto($pdf, 50, 740);
     17 pdf_lineto($pdf, 330, 740);
     18 pdf_stroke($pdf);
     19 PDF_end_page($pdf);
     20 PDF_close($pdf);
     21 fclose($fp);
     22 echo "<A HREF=getpdf.php3>finished</A>";
     23 ?>
     24

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

      1
      2 <?php
      3 $fp = fopen("test.pdf", "r");
      4 header("Content-type: application/pdf");
      5 fpassthru($fp);
      6 fclose($fp);
      7 ?>
      8

Doing the same with pdflib 2.x looks like the following:

Example 2. Creating a PDF document with pdflib 2.x
      1
      2 <?php
      3 $fp = fopen("test.pdf", "w");
      4 $pdf = PDF_open($fp);
      5 pdf_set_info_author($pdf, "Uwe Steinmann");
      6 PDF_set_info_title($pdf, "Test for PHP wrapper of PDFlib 2.0");
      7 PDF_set_info_author($pdf, "Name of Author");
      8 pdf_set_info_creator($pdf, "See Author");
      9 pdf_set_info_subject($pdf, "Testing");
     10 PDF_begin_page($pdf, 595, 842);
     11 PDF_add_outline($pdf, "Page 1");
     12 pdf_set_font($pdf, "Times-Roman", 30, 4);
     13 pdf_set_text_rendering($pdf, 1);
     14 PDF_show_xy($pdf, "Times Roman outlined", 50, 750);
     15 pdf_moveto($pdf, 50, 740);
     16 pdf_lineto($pdf, 330, 740);
     17 pdf_stroke($pdf);
     18 PDF_end_page($pdf);
     19 PDF_close($pdf);
     20 fclose($fp);
     21 echo "<A HREF=getpdf.php3>finished</A>";
     22 ?>
     23
The PHP script getpdf.php3 is the same as above.

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 2.x looks as the following (you can see the same example in the documentation for the clibpdf module):

Example 3. pdfclock example from pdflib 2.x distribution
      1
      2 <?php
      3 $pdffilename = "clock.pdf";
      4 $radius = 200;
      5 $margin = 20;
      6 $pagecount = 40;
      7
      8 $fp = fopen($pdffilename, "w");
      9 $pdf = pdf_open($fp);
     10 pdf_set_info_creator($pdf, "pdf_clock.php3");
     11 pdf_set_info_author($pdf, "Uwe Steinmann");
     12 pdf_set_info_title($pdf, "Analog Clock");
     13
     14 while($pagecount-- > 0) {
     15     pdf_begin_page($pdf, 2 * ($radius + $margin), 2 * ($radius + $margin));
     16
     17     pdf_set_transition($pdf, 4);  /* wipe */
     18     pdf_set_duration($pdf, 0.5);
     19
     20     pdf_translate($pdf, $radius + $margin, $radius + $margin);
     21     pdf_save($pdf);
     22     pdf_setrgbcolor($pdf, 0.0, 0.0, 1.0);
     23
     24     /* minute strokes */
     25     pdf_setlinewidth($pdf, 2.0);
     26     for ($alpha = 0; $alpha < 360; $alpha += 6) {
     27         pdf_rotate($pdf, 6.0);
     28         pdf_moveto($pdf, $radius, 0.0);
     29         pdf_lineto($pdf, $radius-$margin/3, 0.0);
     30         pdf_stroke($pdf);
     31     }
     32
     33     pdf_restore($pdf);
     34     pdf_save($pdf);
     35
     36     /* 5 minute strokes */
     37     pdf_setlinewidth($pdf, 3.0);
     38     for ($alpha = 0; $alpha < 360; $alpha += 30) {
     39         pdf_rotate($pdf, 30.0);
     40         pdf_moveto($pdf, $radius, 0.0);
     41         pdf_lineto($pdf, $radius-$margin, 0.0);
     42         pdf_stroke($pdf);
     43     }
     44
     45     $ltime = getdate();
     46
     47     /* draw hour hand */
     48     pdf_save($pdf);
     49     pdf_rotate($pdf,-(($ltime['minutes']/60.0)+$ltime['hours']-3.0)*30.0);
     50     pdf_moveto($pdf, -$radius/10, -$radius/20);
     51     pdf_lineto($pdf, $radius/2, 0.0);
     52     pdf_lineto($pdf, -$radius/10, $radius/20);
     53     pdf_closepath($pdf);
     54     pdf_fill($pdf);
     55     pdf_restore($pdf);
     56
     57     /* draw minute hand */
     58     pdf_save($pdf);
     59     pdf_rotate($pdf,-(($ltime['seconds']/60.0)+$ltime['minutes']-15.0)*6.0);
     60     pdf_moveto($pdf, -$radius/10, -$radius/20);
     61     pdf_lineto($pdf, $radius * 0.8, 0.0);
     62     pdf_lineto($pdf, -$radius/10, $radius/20);
     63     pdf_closepath($pdf);
     64     pdf_fill($pdf);
     65     pdf_restore($pdf);
     66
     67     /* draw second hand */
     68     pdf_setrgbcolor($pdf, 1.0, 0.0, 0.0);
     69     pdf_setlinewidth($pdf, 2);
     70     pdf_save($pdf);
     71     pdf_rotate($pdf, -(($ltime['seconds'] - 15.0) * 6.0));
     72     pdf_moveto($pdf, -$radius/5, 0.0);
     73     pdf_lineto($pdf, $radius, 0.0);
     74     pdf_stroke($pdf);
     75     pdf_restore($pdf);
     76
     77     /* draw little circle at center */
     78     pdf_circle($pdf, 0, 0, $radius/30);
     79     pdf_fill($pdf);
     80
     81     pdf_restore($pdf);
     82
     83     pdf_end_page($pdf);
     84 }
     85
     86 $pdf = pdf_close($pdf);
     87 fclose($fp);
     88 echo "<A HREF=getpdf.php3?filename=".$pdffilename.">finished</A>";
     89 ?>
     90
The PHP script getpdf.php3 just outputs the pdf document.
      1
      2 <?php
      3 $fp = fopen($filename, "r");
      4 header("Content-type: application/pdf");
      5 fpassthru($fp);
      6 fclose($fp);
      7 ?>
      8

Prev | Home | Next
OCIInternalDebug | Up | PDF_get_info