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


Volomike posted 93 days ago

In fact, the whole Zend Framework library is still pretty primitive and beta, if you ask me. I was expecting a lot more. It's Zend Forms framework (zforms for short) gives you forms like they were made in the 1990s and are very hard to extend. Nowadays, if you want to survive in this industry as a PHP freelancer, I found that jQuery controls added into forms is a necessity in order to remain appealing to clients. Zforms not only is aggravating as heck to use, it makes it hard to extend, insert non-standard controls made with jQuery, and makes one's form look like it was made in the 1990's. We might as well go back to HTML 3.

I also found MVC pretty aggravating to have to live inside. I mean, it's soooo easy to get one's controller logic way out of hand, not pushing enough stuff into the model logic, or making some humongous controller page. So if you're looking to use MVC to eliminate spaghetti code, you might not be getting anywhere. To me, I found it easy to get rid of spaghetti code without MVC, and by separating my XHTML from my PHP and my SQL. For XHTML, I use XHTML files stored as PHP pages in a templates subfolder, and insert PHP Alternative Syntax inside. Why use Smarty? It's redundant. I load these with require_once() instead of $smarty->display(). For SQL separation, I use Outlet or Propel ORM tools. What's left is PHP logic, and I tidy that up by using commented PHP page templates where all I have to do is find the appropriate comment section and insert my code. (That's called "logic templates".) Instead of "model", I simply collect a set of class objects that have worked well for me over the years. Sometimes I reference the methods in them with ->, but most of the time I simply use ::.

So, while someone is off still trying to figure out and fight with ZF, I'm 100 miles away already with faster, leaner code that is elegant to read and extend.


Reece posted 117 days ago

This is a good class - brilliant - are you still going to be adding more to this class?

Many thanks for sharing.

Reece


Reece posted 117 days ago

This is a good class - brilliant - are you still going to be adding more to this class?

Many thanks for sharing.

Reece


Logan Buesching posted 193 days ago

Looking through your implementation, it appears that we have very similar thoughts on how to accomplish the same task (I wrote the Zend_Pdf_Cell implementation).

I was recently contacted by Willie Alberty about my proposal and he mentioned that he's got something cooking that is much more advanced than what I had put in my proposal, and that he was going to be releasing it sometime in the next couple of weeks.

If you are interested in testing and working on what he's got, I would suggest getting in contact with him. I believe his contact information is in JIRA.


Add Comment