Update! (3/31/2008): Now works with Zend Framework 1.5!
Update! (3/19/2008): I have submitted an official proposal for this to be included into the Zend Framework.
Update! (1/12/09): Dominik Deobald was kind enough to supply a UTF character encoding patch and bug fix. I have attached his fix as a .patch file at the end of the post.
I have recently been working with Zend_PDF to create PDF documents. One of the basic requirements for my project is the ability to center text within a screen. Needless to say, I was quite disappointed when I came to find out that Zend_Pdf doesn’t currently have any type of text layout support except for the exact position to place it.
I created a small extension to Zend_Pdf, and building off of an idea from FPDF, I created Zend_Pdf_Cell. Zend_Pdf_Cell is not currently supported by Zend, and is not officially in the Zend Framework, although I have posted my code for them to view it and give feedback (and hopefully have it incorporated into Zend Pdf). My first try I misunderstood parts of Zend_Pdf, but I have gone back and fixed it.
Features
These features have been most used by myself and mostly work.
* The ability to create a cell and place text in it.
* Specify the width and height of a cell
* Position a cell (one or more of these combined)
** To the left
** To the right
** At the bottom
** At the top
** Centered horizontally
** Centered vertically
* Align text within a cell
** Left
** Right
** Centered
** (To be done later) Justify
* Format different parts of the text in different fonts
Experimental
I just recently put these abilities in, so they may not fully work or even work properly.
* Create a border around the cell
* Word wrap text around the cell
Installation
To install this, just place the Cell.php file in Zend/Pdf/, then in your php file, just add:
include_once('Zend/Pdf/Cell.php');
Examples
The following is an example of how to use the Zend Pdf Cell:
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);
//Creates a cell in the specified page
$cell=new Zend_Pdf_Cell($pdf->pages[0]);
//adds a cell in the upper left with "Hello World"
$cell->addText("Hello World");
$cell->write();
//creates a cell in the center of the page
//To do top and right, then you would
//or together POSITION_RIGHT and
//POSITION_TOP.
$cell=new Zend_Pdf_Cell($pdf->pages[0],
Zend_Pdf_Cell::POSITION_CENTER_X |
Zend_Pdf_Cell::POSITION_CENTER_Y);
//add a 1 pixel border
$cell->setBorder(1);
//align to the right
$cell->addText("The quick brown fox jumped over the lazy dog",
Zend_Pdf_Cell::ALIGN_RIGHT);
$cell->write();
?>
If you have any questions or wish to report problems, please use the comment box below. I would also like to hear if you have successfully implemented this!
Files
Cell.php – Zend Framework 1.0.*
Cell.php – Zend Framework 1.5.*
(Updated 1/12/08)
UTF character patch by Dominik