PDFs with PHP part 1 - And Now to Output the Text (Page 6 of 10 )
You will need to pass to this method the x/y position of your text, as well as the actual text.
function text
($x, $y, $text)
{
$text = $this->_escape($text); // Escape any harmful
// characters.
$out = sprintf('BT %.2f %.2f Td (%s) Tj ET',
$x, $this->_h - $y, $text);
$this->_out($out);
}
Note how for simplicity we allow the user to specify the y position measured from the top edge of the paper (whereas in fact PDF measures from the bottom). To achieve this we subtract the y value from the page height in the actual code ($this->_h - $y).
Also note how actual text needs to be escaped to ensure that it is safely inserted into the file. Since text in the PDF file is denoted using parentheses around it, any parentheses in the text itself should be escaped.
The best solution is to create a separate function to handle any cases when text needs to be inserted safely. This will be used a couple of times in this tutorial, but it will also be useful if you add more functionality to this class later.
function _escape
($s)
{
$s = str_replace('\', '\\', $s); // Escape any '\'
$s = str_replace('(', '\(', $s); // Escape any '('
return str_replace(')', '\)', $s); // Escape any ')'
}
Next: Closing the Document >>
More Zend Articles
More By Zend