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.
If compression is required page content will be passed through the gzcompress() function before being written to output. Here you also can see why the $_n object counter starts from 2. We set the root pages parent as object number 1, and later you will see that we set resources as object number 2. This is just so that it is easier for us to reference these when required, for example in each page object.
function _putPages
() { /* If compression is required set the compression tag. */ $filter = ($this->_compress) ? '/Filter /FlateDecode ' : ''; /* Print out pages, loop through each. */ for ($n = 1; $n <= $this->_page; $n++) { $this->_newobj(); // Start a new object. $this->_out('<</Type /Page'); // Object type. $this->_out('/Parent 1 0 R'); $this->_out('/Resources 2 0 R'); $this->_out('/Contents ' . ($this->_n + 1) . ' 0 R>>'); $this->_out('endobj'); /* If compression required gzcompress() the page content. */ $p = ($this->_compress) ? gzcompress($this->_pages[$n]) : $this->_pages[$n]; /* Output the page content. */ $this->_newobj(); // Start a new object. $this->_out('<<' . $filter . '/Length ' . strlen($p) . '>>'); $this->_putStream($p); // Output the page. $this->_out('endobj'); } /* Set the offset of the first object. */ $this->_offsets[1] = strlen($this->_buffer); $this->_out('1 0 obj'); $this->_out('<</Type /Pages'); $kids = '/Kids ['; for ($i = 0; $i < $this->_page; $i++) { $kids .= (3 + 2 * $i) . ' 0 R '; } $this->_out($kids . ']'); $this->_out('/Count ' . $this->_page); /* Output the page size. */ $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]', $this->_w, $this->_h)); $this->_out('>>'); $this->_out('endobj'); }
Let’s look at another method now: _putStream(). We could have included the code in the actual _putPages() function, however, since this method is required for other objects (such as images), we might as well separate it out now.