#native_company# #native_desc#
#native_cta#

Creating and Manipulating PDFs with PHP and FPDF

By W. Jason Gilmore
on March 10, 2011

The Portable Document Standard (PDF) is one of the few standards to achieve a universal level of familiarity among computer users of all levels. Having been created by Adobe Systems almost 20 years ago, freely available PDF readers available for every conceivable computing platform make it possible to deliver pixel-perfect files, which can easily be read, printed, and saved locally for later retrieval. Given such advantages, it’s no wonder enormous organizations such as the Internal Revenue Service have long made it a standard for distributing important documents such as tax forms.
Because the Web has become the primary mechanism for distributing PDF documents, it’s common to encounter questions on various web development forums pertinent to the dynamic creation of PDF documents using languages such as PHP. As it happens, PHP has bundled support for manipulating PDF documents going back at least 10 years, and perhaps even longer. However, this extension requires a library called PDFlib, which is a fairly expensive piece of software (PDFlib 8 costs $1,095 according to the PDFlib website). Mind you, I have absolutely no problems with commercial software; rather I’m only pointing out that this cost is likely out of reach for many VSBs (very small businesses). If you have the means and like what PDFlib has to offer then by all means purchase it. PHP’s PDF extension does also happen to work with a free version of PDFlib called PDFlib Lite, however the PDFlib Lite licensing agreement dictates that the Lite version can’t be used for commercial purposes.
Thankfully, such a demand for PDF manipulation capabilities exists within the PHP community that numerous alternative open source solutions have long been available, including notably FPDF.

Installing FPDF

FPDF is an open source PHP class released under a permissive license, which indicates that the software can be used for any purpose, commercial or otherwise. Bundling its PDF manipulation functionality into a single class, using FPDF is as simple as referencing it within your PHP script using the require statement. Further, although the library is object-oriented, no PHP 5 specific features are required meaning it is compatible with PHP 4 installations on the off chance any still exist.
You can download the latest version from the FPDF website. Unzip the download and place the directory within PHP’s include_path. Presuming you’re modifying the include_path directive from within the php.ini file and PHP is installed as an Apache module, don’t forget to restart your web server.

Creating Your First PDF

With FPDF installed, let’s create a simple PDF file. The following script will create a single-page PDF of paper size A4 and portrait orientation using points as the unit of measurement. On this single page a 16-pt Times New Roman boldface header containing the text PDF Generation Made Simple will be added and centered.

<!--p

  require 'fpdf.php';

  $pdf=new FPDF('P', 'pt', 'A4');
  $pd-->AddPage();
  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0, 10, 'PDF Generation Made Simple!', 0, 0, 'C');
  $pdf->Output();

?>

All of the above method calls should be fairly intuitive except for the Cell() call. As you can see this method accepts six parameters (it actually accepts a total of eight, two of, which I don’t use here; see the FPDF documentation for more details). These six parameters define the cell width (in points, although you can use 0 as I’ve done here, which will cause the cell to extend to the right margin), cell height (in points), cell contents, cell border, position of cursor following the cell placement, and the cell content alignment (C, L, or R).

Adding Multiple Sections of Text

Of course you’ll probably want to add more to a PDF than just a centered header. FPDF offers several approaches to adding multiple sections of text. For instance, if you wanted to output array elements you can use the MultiCell() method,, which will automatically advance the document’s insertion cursor each time it is executed:

<!--p

  require 'fpdf.php';

  $books = array (
    'The Sun Also Rises, by Ernest Hemingway',
    'King Rat, by James Clavell',
    'The Long Tail, by Chris Anderson'
  );

  $pdf=new FPDF('P', 'pt', 'A4');
  $pd-->AddPage();

  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0,10,'My favorite books!', 0, 2, 'C');

  $pdf->SetFont('Times', '', 12);

  foreach ($books AS $book) {
    $pdf->MultiCell(0, 20, $book, 0, 'L');
  }

  $pdf->Output();

?>

Notice how I set the Cell() method’s cursor placement parameter to 2,, which tells FPDF to move the cursor to the next line. See the FPDF documentation for more details.
Several other useful methods exist for adding multiple sections of text. See the ln() and Write() methods for more details.