picture of Luca
PHP tied to PDFLib is perhaps the best platform for Web Publishing. A couple of typical uses:
  1. brochure on demand
  2. e-nvoice: invoice for e-commerce
With this Tutorial you'll learn how to use the PDF extension in PHP4 to create PDF documents.
We also put focus on creating PDF documents with data from MySQL.

Summary of Contents

  1. Installing PDFLib 3.0.1 and PHP4.0.1pl2 with PDF support
  2. Distilling PDF document
I am going to assume a *little* experience in configuring and installing PHP.

Installing PDFLib and PHP with PDF Support

  1. PHP 4.02+ from http://php.net
  2. PDFLib 3.0.1 from http://www.pdflib.com
This is a little recipe to get PDFlib 3.0.1 working with PHP4:
  1. download PHP directly from http://php.net Uwe Steinman patched ext/pdf/pdf.c to support PDFLib v 3.0.1

  2. download PDFLib 3.0.1 from http://www.pdflib.com
    1. apply every patch you find at http://www.pdflib.com/pdflib/patches.html
    2. Configure, Make & install PDFLib
      #./configure --enabled-shared-pdflib
      #make install
    You'll get PDFLib installed in /usr/local/lib .

  3. Configure PHP
           #./configure --with-apxs=/usr/bin/apxs \
              --with-gd  --with-pdflib=/usr/local --with-mysql=/usr/local \
              --with-config-file-path=/etc/httpd  --with-zlib-dir=/usr    \
              --with-ttf=/usr/local/include             \
              --with-jpeg-dir=/usr --with-tiff-dir=/usr \
              --with-system-regex=yes --enable-debug=no
           #make install

  4. Update System Library
           Insert  /usr/local/lib in  /etc/ld.so.conf

  5. Test & Hack
    Now you need to restart Apache
           #apachectl restart
    Copy pdfclock.php in your httpd directory... test... and That's all.

Important Notes

To get working PDFLib with fonts you must pay attention to the UPR section of the PDFLib manual.
A simple way to use fonts with PDFLib is to copy the standard UPR description file included in PDFLib tarball, (fonts/pdflib.upr) into your working directory.

Distilling PDF document

Now we're ready to generate PDF on the fly!
In this little example we'll generate brochure on demand for FlyStore Inc., taking data from a catalogue database.

Prepare Database Test

I am going to assume a *little* experience in databases. Having said that, I'm really only hoping that you know how to create a database and insert tables into that database.
Creating table catalogue:
create table catalogue(
  id           smallint(8) unsigned DEFAULT '0' NOT NULL,
  item         varchar(100)         DEFAULT ''  NOT NULL,
  description  tinytext,
  img_data     longblob,
  imgname      varchar(60),
  imgsize      varchar(60),
  imgtype      varchar(60),
  price        smallint(8) unsigned DEFAULT '0' NOT NULL,
  PRIMARY      KEY (id),
  KEY          item (item(20))

Sending MIME Header Information

In order to have our document show up correctly, we need to send correct header information to the user's browser.
With PHP we can do that using the header function. The following code sends the correct MIME type to the browser.
header( "Content-type: application/pdf" );
header( "Content-Disposition: attachment; filename=modulo.pdf" ); 
header( "Content-Description: PHP3 Generated Data" );

Important Notes

What you need to know is that you must not send anything before header output. A common mistake is the presence of spaces at the beginning of the file.

Getting Data From MySQL

Here we've got a snapshot of code for retrieving data from the catalogue database.


mysql_connect ("""flyadm""flystore")
     or die (
"Could not connect");

mysql_select_db ("flystore"$link);

$result mysql_query ("SELECT * FROM catalogue"$link)
     or die (
"Invalid query");

$data mysql_fetch_row ($result);
mysql_close ($link);


Generating PDF File

In order to generate PDF document, we need to do these steps:
  1. Open a PDF stream and associate a handle with it:
    $pdf = PDF_open();

  2. (Optional) Set document information like Author, Title, Subject, etc

  3. Start a new page (a PDF file can be made of different page with different layout, e.g. portrait, landscape...):
    PDF_begin_page($pdf, 595, 842);
  4. (Optional) Set an hyperlink :
    PDF_add_outline($pdf, "Item ".$data[1]);

  5. Choose font type, dimension
    (pdf_set_font($pdf, "Helvetica-Bold" , 20, winansi);) and rendering mode

  6. Insert text at X,Y position :
    PDF_show_xy($pdf, "Item : " .$data[1], 100, 700);
    insert image at X,Y position on PDF document:
    pdf_place_image($pdf, $im, 100, 300, 3);

  7. Flush text buffer and close PDF stream.

PDF Coordinate Systems

What we need to do to locate a string or picture in some part of the PDF page, is to convert from metric/inch distance to DTP point correspondent value. At page 45 of PDFLib Manual we read:
".. . The default coordinate system (or default user space in PDF lingo) has 
the origin in the lower left corner of the page, and
uses the DTP point as unit:
 1 pt = 1 inch / 72 = 25,4 mm / 72 = 0,3528 mm"
Here is a snapshot code for generating PDF file:


pdf_set_info_author($pdf"Luca Perugini");
PDF_set_info_title($pdf"Brochure for FlyStore");
pdf_set_info_creator($pdf"See Author");
PDF_add_outline($pdf"Item ".$data[1]);
pdf_set_font($pdf"Helvetica-Bold" 20winansi);
pdf_show_xy($pdf"FlyStore Catalogue 2000",50,780);

PDF_show_xy($pdf"Item : " .$data[1], 100700);

PDF_show_xy($pdf"Description : " .$data[2], 100620);

$im PDF_open_jpeg($pdf"pass4_sml.jpg");
pdf_close_image ($im);


In the end, I'd like to remind you that this article is not a PDF Tutorial, so if you need more information about the PDF language and its use, you must have a look at http://www.pdfzone.com/ and http://www.planetpdf.com/.

Files Used in This Tutorial:

I hope this has been of use to you.
-- Luca