PDFs with PHP part 1 - Adding a Page (Page 5 of 10 )
We can now add a page to our document. The following code is quite straightforward.
One point worth noting is the $_font_family check. For any text to be written to a page we need to set the font. However, we have to take into account the possibility that the font was set before any page was added, or that the font was set for a previous page in the current document. Either way we need to check the font class variable, and output the font information to the page. The function setFont() is used for this, which we shall cover later.
function addPage
()
{
$this->_page++; // Increment page count.
$this->_pages[$this->_page] = ''; // Start the page buffer.
$this->_state = 2; // Set state to page
// opened.
/* Check if font has been set before this page. */
if ($this->_font_family) {
$this->setFont($this->_font_family, $this->_font_style, $this->_font_size);
}
}
Output of Simple Text
As mentioned earlier, before any text can be output, font information must be supplied. We therefore need a function to define which font will be used. PDF specifications offer a core set of fonts which can be used with no extra information supplied to the PDF reader. You can also embed your own custom fonts into a PDF file, but for this you need to create font definitions, which are beyond the scope of this tutorial.
For now, limit your output to the following fonts:
- Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique;
- Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique;
- Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic;
- Symbol;
- ZapfDingbats.
The following method sets the font family name, and also (optionally) a style such as bold, italic or both, and a font size.
function setFont
($family, $style = '', $size = null)
{
$family = strtolower($family);
if ($family == 'arial') { // Use helvetica.
$family = 'helvetica';
} elseif ($family == 'symbol' || // No styles for
$family == 'zapfdingbats') { // these two fonts.
$style = '';
}
$style = strtoupper($style);
if ($style == 'IB') { // Accept any order
$style = 'BI'; // of B and I.
}
if (is_null($size)) { // No size specified,
$size = $this->_font_size; // use current size.
}
if ($this->_font_family == $family && // If font is already
$this->_font_style == $style && // current font
$this->_font_size == $size) { // simply return.
return;
}
/* Set the font key. */
$fontkey = $family . $style;
if (!isset($this->_fonts[$fontkey])) { // Test if cached.
$i = count($this->_fonts) + 1; // Increment font
$this->_fonts[$fontkey] = array( // object count and
'i' => $i, // store cache.
'name' => $this->_core_fonts[$fontkey]);
}
/* Store current font information. */
$this->_font_family = $family;
$this->_font_style = $style;
$this->_font_size = $size;
$this->_current_font = $this->_fonts[$fontkey];
/* Output font information if at least one page has been
* defined. */
if ($this->_page > 0) {
$this->_out(sprintf('BT /F%d %.2f Tf ET', $this->_current_font['i'], $this->_font_size));
}
}
The following method enables easier changing between font sizes, without having to go through the whole setFont() function.
function setFontSize
($size)
{
if ($this->_font_size == $size) { // If already current
return; // size simply return.
}
$this->_font_size = $size; // Set the font.
/* Output font information if at least one page has been
* defined. */
if ($this->_page > 0) {
$this->_out(sprintf('BT /F%d %.2f Tf ET',
$this->_current_font['i'],
$this->_font_size));
}
}
Next: And Now to Output the Text >>
More Zend Articles
More By Zend