PDFs with PHP part 1 - Compression (Page 9 of 10 )
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.
function _putStream
($s)
{
$this->_out('stream');
$this->_out($s);
$this->_out('endstream');
}
Next: Resources >>
More Zend Articles
More By Zend