I\'m attempting to dynamically create PDF documents on the server and send them to the client using the Zend_Pdf library. All text on the PDF needs to be center aligned to the p
In case anyone else runs into a similar problem, the issue is here:
function getTextWidth($text, $font, $font_size) {
$drawing_text = iconv('', 'UTF-8', $text);
$characters = array();
for ($i = 0; $i < strlen($drawing_text); $i++) {
$characters[] = (ord($drawing_text[$i++]) << 8) | ord ($drawing_text[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$text_width = (array_sum($widths) / $font->getUnitsPerEm()) * $font_size;
return $text_width;
}
When building the characters array, the characters are being loaded incorrectly - 8 bits, not 16.
$characters[] = ord ($drawing_text[$i]);
This solves the problem, and correctly calculates text width.