PDF and PHP - Creating dynamic PDF documents from your server using PHP

Introduction

This presentation will explore the capabilities of turning your dynamic elements, including database information, into downloadable PDF documents. We will be introducing two open source PDF creation libraries for PHP: FPDF and R&OS. These library functions show the use of dynamic text, drawing, and inserting images for PDF output will be shown. Both of these libraries are open source, free, non-commercial, and non-module. You will not need to install them into PHP, which makes them super-simple to use.

There are other options, namely using the PDFlib library, and some commercial products available. They are not included here because they involve money and installing, which many webmasters on shared servers simply cannot do. The possible performance losses over module-based versus non-module-based are negligible. For our examples, they simply do not matter.

Why use PDF?

I suppose if your life revolves around Microsoft Word, this is a valid question. The answer is simple: you want to share your documents! Adobe created the PDF format with a goal of bridging the OSes, and making PDF cross-platform, and open. The need to get beautiful fonts and content from document creators with Mac desktops to the Windows business desktops was great. It was even greater for professional printers. Adobe's long term commitment to the format has made it a standard, not just on the desktop, but also the web browser itself. For rich documents beyond RTF, PDF has been the clear leader.

Our Test Data

This is a test database for our example. A simple many-to-many, normalized by using a junction table.

people

Field Type
id int
first_name varchar
last_name varchar

places

Field Type
id int
name varchar
area_name varchar

people_places

Field Type
people_id int
places_id int

R&OS

Site: http://www.ros.co.nz/pdf/

R&OS is a base class php module.They have a PDF manual available. There are 15 fonts available to the class for use, and more can be added. There are two basic approaches to doing PDF from your server: stream or file. Let's start with a simple input stream. The source is here.

R&OS has a major extension, ezPdf. We'll use this extension to show our database, starting with a simple table, people. We will place output to the browser during the php run, and have a pdf file left on the server.

Run php and look at output pdf file. See source here.

Now we'll use some joins to show more data.

Run php and look at output pdf file. See source here.

FPDF

Site: http://www.fpdf.org/

FPDF is a PHP class which allows to generate PDF files with pure PHP. One of the great things about thie site and the library is the documentation, which are online. There are different 56 functions in the manual, many with full code examples. There is a tutorials section with seven varying projects coded from from start to finish, with demo page outputs.The FAQs are quite good, and even have workarounds for finnicky versions of IE, especially IE 5.5. They even have a forum and links to FPDF extensions.

FPDF has other advantages: high level functions. Here is a list of its main features:

FPDF requires no extension (except zlib to activate compression) and works with PHP4 and PHP5. Compability is to Acrobat version 5. So what about unicode? FPDF does NOT support unicode, however, there is a free FPDF class extension that has been developed by acko.net, which handles utf-8 encoding.

Minimal Sample

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

See minimal demo

Dynamic Sample

When you put all the pieces together, you can do something like this:

See source code and the result