Where to get and how to use zend_pdf standalone
Asked Answered
M

1

7

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

Multiplier answered 5/2, 2013 at 21:14 Comment(0)
K
10

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!

Krugersdorp answered 6/2, 2013 at 3:47 Comment(1)
Thanks for clarifying the whole Zend_pdf state. The process of using zend framework seems a little too much for what I need. Sorry for being a little impatient on my original post. Basically, I need to write over an existing pdf and attach it to an email using php mailer. It is an online store which is already working, but I need to add the sale of gift certificates. I have a pdf form (I might need to remove the digital form in it) and need to write the valu9e and serial number on it. I think I'll end up using fpdi ( setasign.de/products/pdf-php-solutions/fpdi )Multiplier

© 2022 - 2024 — McMap. All rights reserved.