This tutorial is intended for the PHP programmer who needs to incorporate PDF generation in a script without using external libraries such as PDFlib (often unavailable due to licensing restrictions or lack of funds). This tutorial will cover only the basics, which hopefully will give you a good start. PDF has a vast set of features and possibilities which can not be covered in a short tutorial. If you need more than what is covered here, you might want to look at some similar yet more complete solutions available, such as the excellent work done by Olivier Plathey on the FPDF class (http://fpdf.org), on which this tutorial is based. Of course, you may wish to take your own route and for that there is also the PDF reference (be warned: it’s 1,172 pages!) Basic familiarity with using PHP classes is assumed. Knowledge of PDF file structure is not required, as all references are explained.
Whilst the content streams define the objects on a page, sometimes they need to reference objects outside the content stream. These are called resources. Resources are named objects such as font information or image data. The following method includes any resources defined so far into the main buffer. In this first part of the tutorial fonts are the only resources we will be dealing with.
This last private function, called in the above _putResources() method, includes any font names into the PDF file. As we are only covering core fonts in this tutorial, nothing more than listing the font names is done here.
Document Output The following function, the actual output of the document, does nothing more than make sure the document is closed, send a few headers according to browser type, and echo the buffered data.
function output
($filename) { if ($this->_state < 3) { // If document not yet closed $this->close(); // close it now. } /* Make sure no content already sent. */ if (headers_sent()) { die('Unable to send PDF file, some data has already been output to browser.'); } /* Offer file for download and do some browser checks * for correct download. */ $agent = trim($_SERVER['HTTP_USER_AGENT']); if ((preg_match('|MSIE ([0-9.]+)|', $agent, $version)) || (preg_match('|Internet Explorer/([0-9.]+)|', $agent, $version))) { header('Content-Type: application/x-msdownload'); Header('Content-Length: ' . strlen($this->_buffer)); if ($version == '5.5') { header('Content-Disposition: filename="' . $filename . '"'); } else { header('Content-Disposition: attachment; filename="' . $filename . '"'); } } else { Header('Content-Type: application/pdf'); Header('Content-Length: ' . strlen($this->_buffer)); Header('Content-disposition: attachment; filename=' . $filename); } echo $this->_buffer; }
The Script The complete class You can download the entire class for use with Part 1 of this tutorial. Example Use
<?php require 'PDF.php'; // Require the lib. $pdf = &PDF::factory('p', 'a4'); // Set up the pdf object. $pdf->open(); // Start the document. $pdf->setCompression(true); // Activate compression. $pdf->addPage(); // Start a page. $pdf->setFont('Courier', '', 8); // Set font to arial 8 pt. $pdf->text(100, 100, 'First page'); // Text at x=100 and y=100. $pdf->setFontSize(20); // Set font size to 20 pt. $pdf->text(100, 200, 'HELLO WORLD!'); // Text at x=100 and y=200. $pdf->addPage(); // Add a new page. $pdf->setFont('Arial', 'BI', 12); // Set font to arial bold italic 12 pt. $pdf->text(100, 100, 'Second page'); // Text at x=100 and y=200. $pdf->output('foo.pdf'); // Output the file named foo.pdf ? >