Google QR Codes and Zend_PDF

Now that we can generate QR codes using the Google Chart API, and we have a controller action that serves it up for us with the correct .PNG header (see my previous post), we need to get it where it’s of any use – on paper!

So following on from my last post about Zend_Pdf, we can now do the following to get it in our PDF:

//draw qr code
 $target = 'http://example.co.uk/download/qr?target=http://www.example.co.uk/properties/view/id/'.$prop->getID();
 $img = file_get_contents($target); 
 file_put_contents(APPLICATION_PATH.'/uploads/tmp/'.$prop->getID().'-qr.png',$img);
 $image = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH.'/uploads/tmp/'.$prop->getID().'-qr.png');
 $page->drawImage($image, 13, 13, 94,94);
 unlink(APPLICATION_PATH.'/uploads/tmp/'.$prop->getPid().'-qr.png');

The first line of code is the url of our download controllers QR action.  file_get_contents stores the image in memory, which we then save into a temp folder using file_put_contents. These commands are the equivalent of a lot of fopen etc, so use them if you don’t already!  This example uses my property object to make a dynamic filename, but this isn’t necessary, but I don’t need to tell you that!

We then use Zend_Pdf to load our png, and when drawing an image, the numbers in the argument are x1, y1, x2, y2 being bottom left and top right corners respectively. Finally, ditch the temp file with the unlink command. Have Fun!

Advertisements

One thought on “Google QR Codes and Zend_PDF

  1. If you are using Zend_Matrixcode this will work, took me some time to figure it out…
    $this->code_params [‘text’] = $text;
    $renderer_params = array (‘imageType’ => ‘png’, ‘sendResult’ => false );
    $imageResource = Zend_Matrixcode::render ( ‘qrcode’, $this->code_params, ‘image’, $renderer_params );
    imagepng ( $imageResource, ‘/tmp/qrcode’ . $license->number . ‘.png’ );
    $image = Zend_Pdf_Image::imageWithPath ( ‘/tmp/qrcode’ . $license->number . ‘.png’ );
    //Draw QR Code
    $page->drawImage ( $image, 270, 575, 395, 700 );
    unlink ( ‘/tmp/qrcode’ . $license->number . ‘.png’ );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s