Producing PDF

In this section, we show you how to use R&OS Ltd's free PHP PDF creation library (we refer to this as pdf-php throughout this section). The library has two advantages over other approaches: it's free and it doesn't require any additional PHP configuration. What's more, it's powerful and you can do most things you need, including producing tables containing results from database queries and inserting images into a document.

You can find out more about pdf-php from http://www.ros.co.nz/pdf and you can download the source, documentation, and get involved in the project at http://sourceforge.net/projects/pdf-php/. Instructions for installing pdf-php are included in Appendix A through Appendix C.

Hello, world

Example 13-1 shows a simple PHP example.

Example 13-1. A simple example that produces Hello, world on an A4 page

<?php
  require "class.ezpdf.php";

  // Create a new PDF document
  $doc =& new Cezpdf( );

  // Add text to the document
  $doc->ezText("Hello, world");

  // Output the document
  $doc->ezStream( );
?>

This PHP example produces the PDF document shown in Figure 13-1.

The PDF document produced by Example 13-1

Figure 13-1. The PDF document produced by Example 13-1

The base class for producing PDF files is class.pdf.php , but we've used its extension class.ezpdf.php in our simple example. The extension has several useful utilities for document creation and it still includes all the methods of the base class, and so we recommend ...

Get Web Database Applications with PHP and MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.