Question

I spent the last 2 days looking for answers on how to use zend_pdf as standalone. Everything I found on google refers to a pdf.php that I can't find anywhere.

I have found zend_pdf here ( https://github.com/zendframework/ZendPdf )

I don't want to install the entire framework on the server and I don't want people to tell me to use fpdf or tcpdf or anything else. zend_pdf seems to be the perfect solution. I just want to know how to use it asstandalon.

Any clear instructions?

Thank you

Was it helpful?

Solution

To begin with, it is worth noting that the version of ZendPdf that you are referring to is the version that was originally included in ZF2 but was dropped from the framework some time in 2011. As a side note, I spoke to Matthew Weier O'Phinney (ZF2 project lead) about this at ZendCon 2012 and he said that if anybody wants to put their hand up to maintain this class we can get it back into the ZF2 framework.

The ZF2 version that we're talking about here, although semantically identical to the ZF1 version, is syntactically different due to the fact that the ZF2 developers did the conversion to namespaces and some other refactoring before they decided to split it off from the main project. Most (maybe even all?) of the ZendPdf examples are for Zend_Pdf from ZF1 and they won't work with this version unless you deal with the autoloading and the other syntactical differences.

I hadn't used this standalone version until today and discovered that it DOES NOT RENDER the resulting PDF document unless you include one folder of source code from the ZF2 framework. In order to make the sample code below run, you will need to grab a copy of the ZF2 framework and copy the Zend/Memory folder into the library folder that was created when you downloaded ZendPdf from Github.

Once you've done that, the following stand-alone PHP code will allow you to use ZendPdf to create a PDF file:

<?php
function simpleBootLoader( $stack )
{
    require_once './library/' . 
                 str_replace( '\\', DIRECTORY_SEPARATOR, $stack ) .
                 '.php';
}

spl_autoload_register( 'simpleBootLoader' );

use ZendPdf\PdfDocument;
use ZendPdf\Page;
use ZendPdf\Font;

$pdf = new PdfDocument();

$pdf->pages[0] = new Page( Page::SIZE_A4 );
$pdf->pages[0]->setFont( Font::fontWithName( Font::FONT_HELVETICA ), 24 );
$pdf->pages[0]->drawText( 'Hello world!', 240, 400 );

$pdf->save( 'example.pdf' );

This is not production-ready code by any stretch of the imagination. You'll want a more robust autoloader for starters.

Anyway, I hope this solves your immediate problem and/or gets you on the right track. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top