Gorilla3D Primative Teachings

Joseph Says: STOP SPAMMING MY %$#@ING SITE $%#@!!!!!
Search

Zend_Pdf Extension: Gorilla_Pdf_Page & Gorilla_Pdf_Table


Friday, March 28, 2008



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

  1.  
  2. $pdf = new Zend_Pdf();
  3. $page = new Gorilla_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
  4. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
  5.  
  6. //-- Header
  7. $page->setFont($font, 24);
  8. $page->drawMultilineText(array('Purchase Order'), 390, 48);
  9.  
  10. $page->setFont($font, 10);
  11. $page->setLineWidth(0.5);
  12. $page->drawMultilineText(array('Szzzzice', '9zzzze', 'Szzzzzzzzzzzz26', 'Phone 8555-888-5 Fax 855-4-5554'), 33, 61);
  13.  
  14. $page->drawInfoBox('Date', array('2/29/2008'), 420, 61, 75, 45); // Header Text, Body Text, X, Y, Width, Height
  15. $page->drawInfoBox('PO #', array('000032'), 495, 61, 75, 45);
  16.  
  17. $page->drawInfoBox('Vendor', array('EMC', '3464 Blaire Dr.', 'San Deigo, CA 92081'), 33, 130, 248, 109);
  18. $page->drawInfoBox('Ship To', array('Billy Bob', '453 Wonker Rd.', 'San Deigo, CA 92081'), 320, 130, 248, 109);
  19.  
  20. //-- Draw inventory items
  21. $table = new Gorilla_Pdf_Table($page, 33, 300); // $pdf_page, start x, start y
  22.  
  23. //-- Output the table header
  24. $row = new Gorilla_Pdf_Table_Row();
  25. $col = new Gorilla_Pdf_Table_Column();
  26. $col->setWidth(26)->setText('Qty');
  27. $row->addColumn($col);
  28. $col = new Gorilla_Pdf_Table_Column();
  29. $col->setWidth(93)->setText('Item');
  30. $row->addColumn($col);
  31. $col = new Gorilla_Pdf_Table_Column();
  32. $col->setWidth(183)->setText('Description');
  33. $row->addColumn($col);
  34. $col = new Gorilla_Pdf_Table_Column();
  35. $col->setWidth(93)->setText('Customer');
  36. $row->addColumn($col);
  37. $col = new Gorilla_Pdf_Table_Column();
  38. $col->setWidth(49)->setText('Rate');
  39. $row->addColumn($col);
  40. $col = new Gorilla_Pdf_Table_Column();
  41. $col->setWidth(65)->setText('Amount');
  42. $row->addColumn($col);
  43. $table->addRow($row);
  44.  
  45. //-- Output the inventory
  46. foreach(range(0, 15) as $i) {
  47. $row = new Gorilla_Pdf_Table_Row();
  48. $col = new Gorilla_Pdf_Table_Column();
  49. $col->setWidth(26)->setText(strval($i)); // Qty
  50. $row->addColumn($col);
  51. $col = new Gorilla_Pdf_Table_Column();
  52. $col->setWidth(93)->setText('MAY-ULEX')->setAlignment('center'); // Inventory Code
  53. $row->addColumn($col);
  54. $col = new Gorilla_Pdf_Table_Column();
  55. $col->setWidth(183)->setText('Freedom Task Chair- with Standard Gel Armrests with Matching Textile Cover'); // Inventory Description
  56. $row->addColumn($col);
  57. $col = new Gorilla_Pdf_Table_Column();
  58. $col->setWidth(93)->setText('Billy Bob'); //-- Customer
  59. $row->addColumn($col);
  60. $col = new Gorilla_Pdf_Table_Column();
  61. $col->setWidth(49)->setText('64.00'); // -- Each Price
  62. $row->addColumn($col);
  63. $col = new Gorilla_Pdf_Table_Column();
  64. $col->setWidth(65)->setText(number_format(64 * $i, 2))->setAlignment('right');
  65. $row->addColumn($col);
  66. $table->addRow($row);
  67. }
  68.  
  69. //-- Output the table footer
  70. $row = new Gorilla_Pdf_Table_Row();
  71. $col = new Gorilla_Pdf_Table_Column();
  72. $col->setWidth(468)->setText('Total')->setAlignment('right');
  73. $row->addColumn($col);
  74. $col = new Gorilla_Pdf_Table_Column();
  75. $col->setWidth(65)->setText(number_format(64 * 16, 2))->setAlignment('right');
  76. $row->addColumn($col);
  77. $table->addRow($row);
  78.  
  79. //-- Render the table to the pages/s
  80. $pages = $table->render();
  81.  
  82. //-- If the table overflows onto a new page, they are created
  83. $pdf->pages[] = $page;
  84. foreach($pages as $page) {
  85. $pdf->pages[] = $page;
  86. }
  87.  
  88. $filename = rand().'.pdf';
  89. header('Content-type: application/pdf');
  90. header("Cache-Control: no-cache, must-revalidate");
  91. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  92. echo $pdf->render();
  93.  

Comments


No comments

Add Comment