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
There are a few issues with your question.
Firstly, the
Zend_Pdfyou mentioned above belongs to ZF1, not ZF2. If you really are talking about ZF2, then the class is calledZendPdfand 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 torequirethat 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 inZend_Pdffrom ZF1, not theZendPdfclass 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_oncestatement - it should not have the '.:' included at the start. Now on to my actual answer.Loading
Zend_Pdffrom Zend Framework 1 StandaloneThis should work:
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
ZendPdffrom Zend Framework 2 StandaloneDownload the
ZendPdfproject from github (https://github.com/zendframework/ZendPdf) using the instructions on the project page or clone the repository directly usinggitif you prefer.Change into the directory where
ZendPdfhas been placed and runcomposer installto download the supporting files.In that same directory (ie. the project root of
ZendPdf), create a file calledtest.phpwith the following contents:Run
php test.phpand the example.pdf file should be created.The magic in that second solution comes from
composer, which creates theautoload.phpfile 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.