Zend_Pdf : How to calculate size of text ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Pdf : How to calculate size of text ?

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 ...

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

Reply | Threaded
Open this post in threaded view
|

Re: Zend_Pdf : How to calculate size of text ?

Martijn Korse
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.


SŽébastien Cramatte wrote
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
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Pdf : How to calculate size of text ?

mattmart
In reply to this post by SŽébastien Cramatte
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:

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

2008/11/4 SŽébastien Cramatte <[hidden email]>
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