**I HAVE UPDATED THIS, PLEASE READ THE UPDATE**
I have recently been working with Zend_Pdf and it appears that their ability to put text on a screen is very lacking compared to FPDF. I have recently been diving into expanding the functionality and have provided the following class: Zend_Pdf_Cell. This will allow a developer to create a “cell” within a page, center the cell and align the text within the cell.
The following is a small sample code of how to use the cell:
$pdf=new Zend_Pdf();
$pdf->pages[] =new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC);
$pdf->pages[0]->setFont($font,12);
//create and attach the cell to the first page, and center in the X and Y direction
$cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X | Zend_Pdf_Cell::POSITION_CENTER_Y);
//align the text in the center
$cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
$cell->newLine();
//change the font
$cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD),28);
//align this to the right
$cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
$cell->newLine();
$cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES),10);
//align this to the left
$cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_LEFT);
$cell->newLine();
$cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC),28);
$cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
//finally write the cell to the page. Text will not show up unless you write to the page.
$cell->write();
//create a new cell and center on just the X coordinate
$cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X);
$font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$cell->setPosition(Zend_Pdf_Cell::POSITION_TOP);
$cell->addText("The quick brown fox jumped over the lazy dog.");
$cell->write();
$cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_RIGHT,$pdf->pages[0]->getWidth());
$font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
$cell->setPosition(Zend_Pdf_Cell::POSITION_BOTTOM);
$cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
$cell->write();
$pdf->save($this->uploadDir.'/TEST-CELL2.pdf');
I have attached not only the Cell patch, but also the diff of font resource files separately.
In the origional source for the font’s, we can see that they didn’t use the correct widths:
/* The glyph numbers assigned here are synthetic; they do not match the * actual glyph numbers used by the font. This is not a big deal though * since this data never makes it to the PDF file. It is only used * internally for layout calculations. */
So when attempting to calculate things like a string width, you would get the wrong numbers. I looked up the numbers on Adobe’s Font and Type Technology Center particularly the Unix fonts. From there I generated a small PHP script that would output the ASCII value and the corresponding glyph width. Using these numbers, I updated the Zend font resource files which gives you the diff below. Because these fonts only contain width values for the ASCII numbers 32 - 251ish, those are all that were updated.