PDFs with PHP part 1 - Resources (Page 10 of 10 ) 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. function _putResources () { /* Output any fonts. */ $this->_putFonts(); /* Resources are always object number 2. */ $this->_offsets[2] = strlen($this->_buffer); $this->_out('2 0 obj'); $this->_out('<</ProcSet [/PDF /Text]'); $this->_out('/Font <<'); foreach ($this->_fonts as $font) { $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R'); } $this->_out('>>'); $this->_out('>>'); $this->_out('endobj'); }
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. function _putFonts () { /* Print out font details. */ foreach ($this->_fonts as $k => $font) { $this->_newobj(); $this->_fonts[$k]['n'] = $this->_n; $name = $font['name']; $this->_out('<</Type /Font'); $this->_out('/BaseFont /' . $name); $this->_out('/Subtype /Type1'); if ($name != 'Symbol' && $name != 'ZapfDingbats') { $this->_out('/Encoding /WinAnsiEncoding'); } $this->_out('>>'); $this->_out('endobj'); } }
Document OutputThe 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 ? >
|