The Zend Framework

It's been a long time since I've had the time to post around here.

I'm currently working for a company called Streamflow and its keeping me extremely busy. For Streamflow we do a lot of database to print automation products; specifically db->pdf apps.

This has lead to a very interesting relationship with the guys over at Zend, working on the Zend Framework; specifically Zend_PDF. I've managed to hack in a bunch of image handling code (wrote parsers for TIFF and PNG support for the framework) as well as working really closely with Willie and Alex on overall improvements to the Zend_PDF api.

0.1.4 of the Zend Framework is soon to be released. Hopefully with the new router, but that will certainly have many pdf improvements.

Willie has been slaving away at getting true-type font support added and it should hopefully make it into 0.1.4; if not 0.1.5 for sure.

As for the new router I'll give you guys a quick look at how routes work
Note the class names will probably get updated as its moved to 0.1.4

$router = new Zend_Controller_RewriteRouter();

This initializes the router. This goes in your index.php bootstrap

$router->addRoute('routename',':controller/:action', array('controller'=>'index','action' => 'index'));

This will add a sane default router. Says /controller/action at your url will goto the IndexController and call the IndexAction if there is no variables provided. If they are provided then it will map the first dir level to controller and the second to action making for a url /controllername/actioname

$router->addRoute('routename','catalog/:category', array('controller'=>'catalog','action' => 'index'));

This is where the real power is, instead of mapping /catalog/whatever to the whatever action of the catalog controller, this will map to the index action, but pass along a variable :category as a parameter. I'll show you how to use thatin a minute; for now complete the index.php bootstrap with

$ctrl = Zend_Controller_Front::getInstance();

$view = new Zend_View;
$view->setScriptPath($basePath. '/application/views');
Zend::register('view', $view);

Zend_Controller_Front::run($basePath . '/application/controllers');

Next, we'll create the catalog controller.

class CatalogController extends Zend_Controller_Action
 public function indexAction() {
  $view = Zend::registry('view');
  $params = new Zend_Filter_Input($this->_getAllParams());
  if($category = $params->testInt('category')) {
   $view->category = $category;
           echo $view->render('template.php');
  } else {
   //bad category

In the above code note that I retrieve the 'category' variable by calling testInt on the Zend_Filter_Input class. Category maps to the name given in the route we defined earlier.

Next, creating a PDF

$pdf= new Zend_Pdf();
$pdf->pages[] = ($currentpage = $pdf->newPage('A4'));
$pagewidth = $currentpage->getWidth();
$pageheight = $currentpage->getHeight();
$style=new Zend_Pdf_Style();
$style->setFont(Zend_Pdf_FontFactory::fontWithName(Zend_Pdf_Const::FONT_HELVETICA_BOLD), 5);
$image = Zend_Pdf_ImageFactory::factory('/path/to/file.jpg.png.or.tiff');
//Remeber pdf co-ords start at the bottom left. Use $pageheight in the drawing to start at the top) Also remember that drawing isnt done in pixels on a pdf
//However, that said, if you draw the pixels, the aspect ratio will be maintained.
$currentpage->drawImage($image, $x, $pageheight, $image->getPixelWidth(), $image->getPixelHeight());
//Draw some text beside the image
//Change fonts to a ttf font
$font = Zend_Pdf_FontFactory::fontWithPath($file);
$style->setFont($font, 20);
//Draw some text below and to the side
//Actually render the pdf
$pdfString = $pdf->render();
header("Content-Type: application/pdf");
echo $pdfString;

Hope that helps explain some of the upcoming changes in 0.1.4