I'm trying to install Zend PDF in order to fill out editable PDFs on my client's shared hosting account (media temple). I have it enabled now, confirmed in phpinfo http://i.imgur.com/lDiLk.png but after that, I can't find out what I need to get started. If I try loading the Zend_Pdf class, I receive a "Fatal error: Class 'Zend_Loader' not found" message.
These are very different. Zend Guard Loader is used to run PHP scripts encoded by Zend Guard. And Zend Loader component is used, among other things, to simplify the development. For example, this...
$pdf = new Zend_Pdf(); // what's Zend_Pdf, people?
... statement is meaningless to PHP unless it knows what is Zend_Pdf class. Thankfully, in PHP there's a special mechanism of importing these files automatically - autoloading. Here's how to do it with Zend_Loader:
set_include_path(
implode(PATH_SEPARATOR, array(
get_include_path(),
PATH_TO_ZF_LIBRARY
)));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
...
$pdf = new Zend_Pdf(); // no error, as correct file should be imported automatically
Actually, if it's only a single file that will use Zend component, it's not required that you use the autoloading mechanism. A simple require_once (no pun intended) would suffice:
require_once 'Zend\Pdf.php';
...
$pdf = new Zend_Pdf(); // oh, now I know all about Zend_Pdf!
Zend Guard Loader and Zend Loader are not the same thing. Zend Guard is another Zend product and has nothing to do with Zend Framework. Zend Loader is the name of the class loader within Zend Framework.
Make sure that you have a copy of Zend Framework within your application and that you've setup the include path to point at this location.
Related
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_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.
is it compulsory to use zend framework structure for using zend library or we can use zend library in our own php application...
Three steps to using the ZF (assuming ZF1) library in your own app without the entire ZF MVC stack.
Set the include path
Make sure that the Zend library folder is on your php include_path.
Load your class
You could just include each class file before you use it:
require_once 'Zend/Validate/EmailAddress.php';
$validator = new Zend_Validate_EmailAddress();
But it's a pain to do it that way. Typically, it's better to using the autoloader. The easiest way is something like (early in your bootstrap process, perhaps in a common.php file, YMMV):
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Once this is done, you can instantiate/reference on-demand:
$validator = new Zend_Validate_EmailAddress();
Instantiate/Reference
$validator = new Zend_Validate_EmailAddress();
echo $validator->isValid('test#example.com') ? "Cool" : "Fail";
I developing a project using Zend framework and I came across the following problem. I'm using Zend framework MVC folder structure generated using their zf.sh script.
My library folder has the Zend library folder and it's classes can be called normally inside the application. I created another folder inside my library for my classes. This is the folder structure now:
MyProject
|_application
|_docs
|_public
|_library
|_Zend
|_Buyers
|_Donations.php
|_scripts
I named my Donation class "Buyers_Donations" as the Zend framework naming convention.
When I tried using this class inside my controller
$obj= new Buyers_Donation();
it gave an error can not find class Buyers_Donation inside the controller.
But when I added the following line in my Bootstrap it worked:
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$moduleLoder = new Zend_Application_Module_Autoloader(
array(
'namespace'=>'',
'basePath'=>dirname(__FILE__)
));
Could someone please explain what actually happened and what is the use of the module autoloader although I don't have any modules in my application ?
As you suspected, you shouldn't be using the module autoloader since you're not using modules. Assuming the Zend* classes are autoloading correctly for you, all you need to do is tell the standard autoloader that it should also be used for classes in your 'Buyers' namespace. So instead of the code snippet you posted, just do:
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Buyers_');
you can also set this in application.ini if you prefer.
I'm also assuming that your classes are in the library folder, and not in the public directory as your question implies (this would be bad).
If you do not wish to use the zend's auto loading feature, you will have to include files manually by using require_once(), such as:
require_once 'Buyer/Donations.php';
If you do wish to use zend loader with your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method. in the bootstrap, you could do so as follows:
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance()->
registerNamespace('Buyers_')
return $autoloader;
}
If the auto loader doesn't work, make sure you set the include path to the library folder somewhere. It's automatically added by the zend framework to public/index.php:
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
I want to know how can i use Zend Framework in my php project where my web host doesnt support it. Im using only bunch of the Zend classes, so can I put them inside my include folder and upload it to my site? Will this work?
First include autoloader class.
require_once 'lib/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->registerNamespace('Http\PhpEnvironment', 'lib/Zend/Http');
//Register with spl_autoload:
$loader->register();
and then use below line
$a = new Zend\Http\PhpEnvironment\Request();
echo $a->getQuery()->get();
this is just the demo that you can use loosly coupled zend library to your project.
Here is great place to get started http://framework.zend.com/manual/en/
If you host supports PHP 5, then Zend Framework will work without any problems.
Usually Zend directory is placed inside of lib directory, look at Quick Start.
How to use zend library without using zend framework installation?
I am trying to use zend library(Mail and Mime) without zend framework installation, its not returning any error messages...
but for my project i'm using Mail and Mime library only, How to use Zend Library without installing zend framework ..
Thanks,
Vinoth S
Download Zend Framework and put it into a folder accessible by your PHP. Then either do
include '/path/to/folder/containing/Zend/lib/Zend/Mail.php';
include '/path/to/folder/containing/Zend/lib/Zend/Mime.php';
$mailer = new Zend_Mail;
Or - better and more conventient - setup your autoloader and/or include path so PHP can find the classes directly, without you having to include them.
Also see
the requirements appendix for a detailed list of requirements for Zend Framework.
Register the autoloader and set include path like this:
set_include_path(implode(PATH_SEPARATOR, array(
realpath('./library'),//the path
get_include_path(),
)));
require "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
I've done it more than once to integrate zend libs in other non-zend projects.
Autoloader is not suggested for just inclusion of some libraries as it involves in worse performances (see zend reference about |end_Loader for that).
The best way (from both clear code and performances point of view) is very simple:
1) set the include path: (necessary or you'll have fatal inclusion errors):
set_include_path(implode(PATH_SEPARATOR, array(
'/',
get_include_path(),
)));
2) do a "require_once" of the library/ies you need, following the structure Zend/
e.g:
require_once "Zend/Mail.php";
//you can use now Zend_Mail* classes
note1: you don't have to place a "require_once" of all the needed classes, the main included class already do a require_once of the dependent classes.