At work there is a growing need to move into pdf creation, since we generate all of our invoices via HTML. Some clients need precision on form creation. PDF offers that but Zend_Pdf lib is very limited. So I implemented text-wrapper and table class/functions to help guide me. Its far from done, but a good start to blog about it.
download Gorilla_Pdf file
$pdf = new Zend_Pdf();
$page = new Gorilla_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
//-- Header
$page->setFont($font, 24);
$page->drawMultilineText(array('Purchase Order'), 390, 48);
$page->setFont($font, 10);
$page->setLineWidth(0.5);
$page->drawMultilineText(array('Szzzzice', '9zzzze', 'Szzzzzzzzzzzz26', 'Phone 8555-888-5 Fax 855-4-5554'), 33, 61);
$page->drawInfoBox('Date', array('2/29/2008'), 420, 61, 75, 45); // Header Text, Body Text, X, Y, Width, Height
$page->drawInfoBox('PO #', array('000032'), 495, 61, 75, 45);
$page->drawInfoBox('Vendor', array('EMC', '3464 Blaire Dr.', 'San Deigo, CA 92081'), 33, 130, 248, 109);
$page->drawInfoBox('Ship To', array('Billy Bob', '453 Wonker Rd.', 'San Deigo, CA 92081'), 320, 130, 248, 109);
//-- Draw inventory items
$table = new Gorilla_Pdf_Table($page, 33, 300); // $pdf_page, start x, start y
//-- Output the table header
$row = new Gorilla_Pdf_Table_Row();
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(26)->setText('Qty');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(93)->setText('Item');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(183)->setText('Description');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(93)->setText('Customer');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(49)->setText('Rate');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(65)->setText('Amount');
$row->addColumn($col);
$table->addRow($row);
//-- Output the inventory
foreach(range(0, 15) as $i) {
$row = new Gorilla_Pdf_Table_Row();
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(26)->setText(strval($i)); // Qty
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(93)->setText('MAY-ULEX')->setAlignment('center'); // Inventory Code
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(183)->setText('Freedom Task Chair- with Standard Gel Armrests with Matching Textile Cover'); // Inventory Description
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(93)->setText('Billy Bob'); //-- Customer
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(49)->setText('64.00'); // -- Each Price
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(65)->setText(number_format(64 * $i, 2))->setAlignment('right');
$row->addColumn($col);
$table->addRow($row);
}
//-- Output the table footer
$row = new Gorilla_Pdf_Table_Row();
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(468)->setText('Total')->setAlignment('right');
$row->addColumn($col);
$col = new Gorilla_Pdf_Table_Column();
$col->setWidth(65)->setText(number_format(64 * 16, 2))->setAlignment('right');
$row->addColumn($col);
$table->addRow($row);
//-- Render the table to the pages/s
$pages = $table->render();
//-- If the table overflows onto a new page, they are created
$pdf->pages[] = $page;
foreach($pages as $page) {
$pdf->pages[] = $page;
}
$filename = rand().'.pdf';
header('Content-type: application/pdf');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
echo $pdf->render();
« Back to my notebook