Вот то, что я использую ... -2 просто произвольно, но выглядит нормально.
О, и это часть моего метода для центрирования текста, но вы можете вытащить его.
/**
* Draw text aligned to the right
* @param Zend_Pdf_Page $page page
* @param string $text - text to draw
* @param float $right - right position
* @param float $top - top position
* @param boolean $underline - whether or not to underline the text
*/
public function drawTextCentered(Zend_Pdf_Page $page, $text, $top, $underline = false)
{
$textWidth = $this->getTextWidth($text, $page->getFont(), $page->getFontSize());
$left = ($page->getWidth() - $textWidth) / 2;
$page->drawText($text, $left, $top);
if ($underline) {
$page->drawLine($left, $top-2, $left+$textWidth, $top-2);
}
}
/**
* Return the width of generated string in points
* @param string $text text
* @param Zend_Pdf_Resource_Font $font font
* @param integer $fontSize font size
*
* @return float text width
*/
public function getTextWidth($text, Zend_Pdf_Resource_Font $font, $fontSize)
{
$text = iconv('', 'UTF-16BE', $text);
$chars = array();
for ($i = 0; $i < strlen($text); $i++) {
$chars[] = (ord($text[$i++]) << 8) | ord($text[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($chars);
$widths = $font->widthsForGlyphs($glyphs);
return (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
}