PDFs with PHP part 1 - The Factory Method (Page 3 of 10 )
This method will give us the PDF object with which we can build our document. It sets the initial values for the document, such as page orientation and size, and returns the object.
function
&factory($orientation = 'P', $format = 'A4')
{
/* Create the PDF object. */
$pdf = &new PDF();
/* Page format. */
$format = strtolower($format);
if ($format == 'a3') { // A3 page size.
$format = array(841.89, 1190.55);
} elseif ($format == 'a4') { // A4 page size.
$format = array(595.28, 841.89);
} elseif ($format == 'a5') { // A5 page size.
$format = array(420.94, 595.28);
} elseif ($format == 'letter') { // Letter page size.
$format = array(612, 792);
} elseif ($format == 'legal') { // Legal page size.
$format = array(612, 1008);
} else {
die(sprintf('Unknown page format: %s', $format));
}
$pdf->_w = $format[0];
$pdf->_h = $format[1];
/* Page orientation. */
$orientation = strtolower($orientation);
if ($orientation == 'l' || $orientation == 'landscape') {
$w = $pdf->_w;
$pdf->_w = $pdf->_h;
$pdf->_h = $w;
} elseif ($orientation != 'p' && $orientation != 'portrait') {
die(sprintf('Incorrect orientation: %s', $orientation));
}
/* Turn on compression by default. */
$pdf->setCompression(true);
return $pdf;
}
Also in this method we turn on compression by default. This makes the output PDF files a lot smaller.
The actual setCompression() method is as follows:
function setCompression
($compress)
{
/* If no gzcompress function is available then default to
* false. */
$this->_compress = (function_exists('gzcompress') ? $compress : false);
}
However, whilst learning you may wish to explicitly turn off compression, so that you can open your created PDF document with a text editor and see easily what is happening.
Next: Writing Content >>
More Zend Articles
More By Zend