PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Text Drawing

Text drawing operations also exist in the context of a PDF page. You can draw a single line of text at any position on the page by supplying the x and y coordinates of the baseline. Current font, font size and page fill color are used for text drawing operations (see detailed description below).

<?php
/**
 * Draw a line of text at the specified position.
 *
 * @param string $text
 * @param float $x
 * @param float $y
 * @param string $charEncoding (optional) Character encoding of source
 *               text.Defaults to current locale.
 * @throws Zend_Pdf_Exception
 * @return Zend_Pdf_Page
 */
public function drawText($text$x$y$charEncoding '');

Example 664. Draw a string on the page

<?php
...
$pdfPage->drawText('Hello world!'72720);
...

Example 665. Set font color

<?php
...
$pdfPage->setFillColor(Zend_Pdf_Color_Html::color('#990000'))
        ->
drawText('Hello world (in red)!'72720);
....

By default, text strings are interpreted using the character encoding method of the current locale. if you have a string that uses a different encoding method (such as a UTF-8 string read from a file on disk, or a MacRoman string obtained from a legacy database), you can indicate the character encoding at draw time and Zend_Pdf will handle the conversion for you. You can supply source strings in any encoding method supported by PHP's iconv() function:

Example 666. Draw a UTF-8-encoded string on the page

<?php
...
// Read a UTF-8-encoded string from disk
$unicodeString fread($fp1024);

// Draw the string on the page
$pdfPage->drawText($unicodeString72720'UTF-8');
...

Zend Framework