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.

Adding Images

To add an image to your PDF file, use the Image() method, as demonstrated here:
<!--p

  require 'fpdf.php';

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

  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

  $pdf->Image('easypaypalwithphp.jpg');

  $pdf->Output();

?>
The Image() method is surprisingly powerful in the sense that although it accepts quite a few input parameters, all are optional except for the image file name. It will automatically determine the image's dimensions and image type (JPEG or PNG for instance), however you can override these defaults using other available parameters. See the Image() documentation for all the details.

Adding a Watermark

When making sensitive or copyright material available to customers, it's often useful to include a watermark, which identifies the individual downloading the material. I call this SRM, or Shame Rights Management, because if the customer ever decides to upload the material to a file sharing service, he does so knowing that his name, e-mail address, or other identifying details are embedded in the document.
You can easily embed information in the footer of every generated PDF page using a slick FPDF feature, which merely requires you to extend the FPDF class and override the Footer() method, within it defining what you would like to appear in the page footer. FPDF will automatically detect this method and execute it each time a new page is created. For instance, the following example will embed the e-mail address jason@example.com into the PDF footer:
<!--p

  require 'fpdf.php';

  class WJGPDF extends FPDF
  {

    function Footer()
    {

      $thi-->SetY(-25);

      $this->SetFont('Times', 'B', 12);  

      $this->Cell(0,20,'Licensed to jason@example.com', 0, 0, 'C');

    }

  }

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

  $pdf->SetFont('Times', 'B', 16);
  $pdf->Cell(0,10,'Easy PayPal with PHP', 0, 2, 'C');

  $pdf->Output();

?>

Conclusion

As you can see, the FPDF library packs quite a punch, and I've hardly scratched the surface in terms of what it can do! Be sure to check out the FPDF website for tutorials, documentation, and other useful information.

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.