Discussion:
Zend_Pdf : How to calculate size of text ?
SŽébastien Cramatte
2008-11-04 13:58:06 UTC
Permalink
Hello,

I need to use Zend_Pdf to make an invoice.
How I can calulate width of a "text" with a specific font and size ?

I've tried the method behind
(http://devzone.zend.com/article/2525-Zend_Pdf-tutorial)
I've extend Zend_Pdf class and append the method ...

I've tried to call the method as this :

$this->widthForStringUsingFontSize($cell, Zend_Pdf_Font::FONT_HELVETICA, 8);


It works but seems that returns incorrect value because when I draw a vertical line
to this same position, text and line are aligned !


public 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;

}


I've got the same problem when I try to
right align data. As returned with is incorrect I'm not be abble to align my cells !

Any ideas ?

Regards
Martijn Korse
2008-11-05 08:11:59 UTC
Permalink
I'm not sure what the problem could be ....
I've also used that function, after reading this:
http://framework.zend.com/issues/browse/ZF-313

And created this class with it:
http://devshed.excudo.net/scripts/php/source/excudo+pdf+textblock

Right aligning the text for example - which gives you problems - worked
perfectly for me. So, maybe have a look at my class and see if that works
for you. If it does you can work from there to see what the problem in your
code is.
Post by SŽébastien Cramatte
Hello,
I need to use Zend_Pdf to make an invoice.
How I can calulate width of a "text" with a specific font and size ?
I've tried the method behind
(http://devzone.zend.com/article/2525-Zend_Pdf-tutorial)
I've extend Zend_Pdf class and append the method ...
$this->widthForStringUsingFontSize($cell, Zend_Pdf_Font::FONT_HELVETICA, 8);
It works but seems that returns incorrect value because when I draw a vertical line
to this same position, text and line are aligned !
public 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;
}
I've got the same problem when I try to
right align data. As returned with is incorrect I'm not be abble to align my cells !
Any ideas ?
Regards
-----
http://devshed.excudo.net http://devshed.excudo.net
--
View this message in context: http://www.nabble.com/Zend_Pdf-%3A-How-to-calculate-size-of-text---tp20322766p20337581.html
Sent from the Zend MFS mailing list archive at Nabble.com.
Matheus Martins
2008-11-22 22:18:03 UTC
Permalink
Hello everybody,

Maybe you want to solve this with Zend_Pdf,
but if you need to solve this as soon as possible, I have a tip for you:

I've been working on a task which should parse an HTML file to PDF,
and I found a php class that is the best I've ever saw:
mPDF - http://mpdf.bpm1.com

It extends FPDF (fpdf.org - a great php class)

Different from others, this class doesn't require a lot of memory from your
server.
Actually, we are using less than 32MB* for 200+ pages and almost 500 images
(most are small images, but... you never know).
*Just remind to split your HTML into as many parts as possible (parse HTML
is not that easy nor fast)

If using Zend_Pdf is not essential for you, I would strongly recommend using
mPDF.

Anyway, I am just sharing a great pdfclass with you all.

Best regards,
Matheus Martins
Post by SŽébastien Cramatte
Hello,
I need to use Zend_Pdf to make an invoice.
How I can calulate width of a "text" with a specific font and size ?
I've tried the method behind (
http://devzone.zend.com/article/2525-Zend_Pdf-tutorial)
I've extend Zend_Pdf class and append the method ...
$this->widthForStringUsingFontSize($cell, Zend_Pdf_Font::FONT_HELVETICA, 8);
It works but seems that returns incorrect value because when I draw a
vertical line to this same position, text and line are aligned !
public 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;
}
I've got the same problem when I try to right align data. As returned with
is incorrect I'm not be abble to align my cells !
Any ideas ?
Regards
Loading...