Search code examples
phppdfzend-frameworkzend-pdf

Using Zend_PDF and Zend Framework, Loading Libraries in PHP?


I’m new to PHP, only having worked with a LAMP stack a little bit. I would like to use Zend_PDF to fill forms using PHP. I am having issues and believe I do not know how to properly load Zend Framework (or external libraries in general). Here is what I have currently:

<?php 
include_path='.:/var/www/html/lib/'; 
require_once'.:/var/www/html/lib/Zend/Loader/ClassMapAutoloader.php';
$pdf = Zend_Pdf::load('.:/var/www/html/forms/test.pdf');

echo count($pdf->pages);
?>

I was using count(); as a test but I’m looking to use the setTextField(); functions. I do not have a “/Loader/Autoloader.php” as referenced in some guides.

How do I properly load the library so that I can use the setTextField(); function?

PHP 5.4.16, Zend Framework 2.4.4, CentOS7


Solution

  • There are a few issues with your question.

    Firstly, the Zend_Pdf you mentioned above belongs to ZF1, not ZF2. If you really are talking about ZF2, then the class is called ZendPdf and can be used as a standalone component - you do not need to have a full copy of ZF2 available for the autloading (composer will generate an autoloader - you will just need to require that in your script). Last time I checked (which, admittedly, was a couple of years ago), the two versions were functionally equivalent, so you should probably just use the version that matches the version of Zend Framework that you're actually using.

    Which brings me to the next issue. Because I wasn't completely sure which version you were referring to, I did a quick text search and discovered that the setTextField() method only exists in Zend_Pdf from ZF1, not the ZendPdf class that is related to ZF2, so I'm not sure why you mentioned ZF2 in your question. But anyway, I figured out how to get the ZF2 version working before I made that discovery, so I've included both methods below for completeness.

    Also, you have an error in your require_once statement - it should not have the '.:' included at the start. Now on to my actual answer.

    Loading Zend_Pdf from Zend Framework 1 Standalone

    This should work:

    set_include_path( '/path/to/zf1/library' . PATH_SEPARATOR . get_include_path());
    require_once( '/path/to/zf1/library/Zend/Loader/Autoloader.php' );
    Zend_Loader_Autoloader::getInstance();
    
    $pdf = new Zend_Pdf();
    
    $pdf->pages[0] = new Zend_Pdf_Page( Zend_Pdf_Page::SIZE_A4 );
    $pdf->pages[0]->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 24 );
    
    $pdf->pages[0]->drawText( "Hello world!", 240, 400 );
    
    $pdf->save( 'example.pdf' );
    

    You will obviously need to adjust the paths in the code above in order to make it work properly, but once you do that and run the script, you should end up with example.pdf being created.

    Loading ZendPdf from Zend Framework 2 Standalone

    Download the ZendPdf project from github (https://github.com/zendframework/ZendPdf) using the instructions on the project page or clone the repository directly using git if you prefer.

    Change into the directory where ZendPdf has been placed and run composer install to download the supporting files.

    In that same directory (ie. the project root of ZendPdf), create a file called test.php with the following contents:

    require 'vendor/autoload.php';
    
    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' );
    

    Run php test.php and the example.pdf file should be created.

    The magic in that second solution comes from composer, which creates the autoload.php file for you. There are many, many benefits to using this approach, one of which is that you don't have to have a full copy of Zend Framework 2 installed simply to get a working autoloader.