有一种方法可以精确计算宽度,而不是使用Gorilla3D 的最坏情况算法。
试试这个来自http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535的代码
我在我的应用程序中使用它来计算右对齐文本的偏移量并且它有效
/**
* Returns the total width in points of the string using the specified font and
* size.
*
* This is not the most efficient way to perform this calculation. I'm
* concentrating optimization efforts on the upcoming layout manager class.
* Similar calculations exist inside the layout manager class, but widths are
* generally calculated only after determining line fragments.
*
* @link http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535
* @param string $string
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize Font size in points
* @return float
*/
function widthForStringUsingFontSize($string, $font, $fontSize)
{
$drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
$characters = array();
for ($i = 0; $i < strlen($drawingString); $i++) {
$characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
return $stringWidth;
}
With regard to performance, I haven't used this intensively in a script but I can imagine it's slow. I'd suggest writing the PDFs to disk, if possible, so repeat views are very fast, and caching/hard coding data where possible.