Using Zend_PDF and Zend Framework, Loading Libraries in PHP? - 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

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.

Related

what i need to consider when building phar file from php application use php framework like laravel

I am php developer use laravel-4 as framework for building web applications , in the last few days I wanted to create phar file from my web application created on laravel framework .
I searched in the web for tools build php archive files (.phar) and I found
PHP box , this tool is very good and use json configuration file for building the phar files but i could not use it for creating my phar files because there is many considerations when creating phar file from a web application use a framework like laravel . my questions are :
laravel use composer autoloader as auto loading mechanism
1- how to handle composer auto loading mechanism ?
2- how to handle the framework bootstrapping process ? 'like laravel'
3- what i need to make the browser read my index page from inside the phar file ?
4- how to use framework command line tools from the phar file ? 'like laravel-artisan'
You might use composer.json in your application and require box there.
When your application runs on laravel, you know that your bootstrap works and would also work inside a phar.
I believe Box brings a lot of the composer autloading stuff itself, so you won't run into trouble with it. I think the class_map gets included automatically.
One thing to consider is, that configuration details must be passed in!
In general, you need to "forward" to your application, which is inside the phar, like so:
<?php
require_once "phar://myapp.phar/frontcontroller.php"; // maybe index.php
$config = array('dsn' => 'database-config');
Application::run($config);
Also accessing a PHAR in a PHAR is a problem!
You can't access a PHAR packaged in a PHAR directly.
Firstly you need to extract the packaged PHAR, secondly do the forwarding call and pass the CLI commands along. Problem solved here: https://stackoverflow.com/a/13537329/1163786
Full Example
box.json.dist
{
"main": "bootstrap.php",
"output": "application.phar",
"compactors": ["Herrera\\Box\\Compactor\\Composer"],
"chmod": "0755",
"directories": ["src/"],
"stub": true
}
bootstrap.php
<?php
require 'vendor/autoload.php'; //<-- this is autoload.php generated by Composer
use MyApp\Application;
$config = parse_ini_file(__DIR__.'/config.ini');
$app = new Application();
$app->run($config);

How to use ZF2 ZendPdf in Yii Framework

I want to parse a PDF document in my project, I'm using Yii framework and I'm able to load ZendFramework 1.12, actually I'm using Zend_Lucene and Zend_Mail successfully, but ZendPdf fails to parse the PDF, so I wanted to try with ZF2 (ZendFramework 2), but I'm not able to make it work... I just downloaded the ZF2 library and added the following to my base Controller:
public function __construct($id,$module=null){
Yii::import('application.vendors.ZendFramework.library.*');
Yii::setPathOfAlias('Zend',Yii::getPathOfAlias('application.vendors.ZendFramework.library.Zend'));
parent::__construct($id,$module);
}
So every time I use a Controller this code is executed. Then in the controller I have:
use ZendPdf\PdfDocument;
use Zend\Mail;
class AjaxController extends Controller{...
...
public function actionTestPdf(){
$filepath = realpath('./path/').'/pdftest2.pdf';
$pdf = PdfDocument::load($filepath);
}
When I run the controller: /ajax/TestPdf
I get: Fatal error: Class 'ZendPdf\PdfDocument' not found
What am I doing wrong?
I'd suggest you to install your ZF2 stuff with composer and include the autoloader from composer to get PSR compatible autoloading.
Alternative 1)
You may also try pointing an alias ZendPdf\PdfDocument to the class file location.
Alternative 2)
import the needed classes or directories in your application config.
In this particular case I would recommend NOT using ZendPdf from ZF2, because it is functionally equivalent to Zend_Pdf from ZF1 and so all you're going to do is add unnecessary complexity with classloading, etc, but not solve your underlying problem, as the document will still most likely NOT load using ZendPdf from ZF2.
You should investigate the underlying reason why the document is failing to load in ZF1.

Getting started with Zend PDF and Zend Guard Loader

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.

PHP - ZendFramework: How to use Zend_Translate without the whole framework?

i'm building a simple PHP website and want to translate it into 2 languages (spanish,english).
I've read some questions here and everybody recommend Zend_Translate. I've read the documentation and seems pretty good.
I've read I can use the Zend_Translate component without using the entire Framework, just that component, but i cannot. I've tryed everything. I downloaded the framework and placed it in a libs subdirectory. And i've tryed severals ways to import it:
// First try
require('libs/Zend/Translate.php'); //Fail
//Second try
require('libs/Zend/Loader.php'); //Good
Zend_Loader::loadClass('Zend_Translate'); //Fail
Can you help me please?
Thanks to this post, I was able to finally make it work!
For people like me who are searching for a working example of using zend_translate without using the Zend Framework, here it is:
https://github.com/26medias/zend_translate
If you are using Windows (like I do), you'll need to install gettext first:
http://gnuwin32.sourceforge.net/packages/gettext.htm
To edit the translations:
open /locale/[locale name]/messages.po
add your translations
Execute translate.bat (if you didn't install gettext in the default directory, update the path to the bin folder). It will generate/update the messages.mo file.
And you're done!
Per #santiagobasulto, I'm creating an answer for this question.
Make sure that Zend/ is in your path, as the Zend Framework expects that folder to be there.
You have to put your 'libs' in your include path. It's quite simple :
set_include_path( implode( PATH_SEPARATOR, array(
'/path/to/your/libs',
get_include_path(),
)
)
);

How can i use Zend framework in my php project?

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.

Categories