[英]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. 我是PHP新手,只使用过LAMP堆栈。 I would like to use Zend_PDF to fill forms using PHP.
我想使用Zend_PDF使用PHP填写表格。 I am having issues and believe I do not know how to properly load Zend Framework (or external libraries in general).
我遇到问题,并且相信我不知道如何正确加载Zend Framework(或一般而言的外部库)。 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();
我正在使用
count();
as a test but I'm looking to use the setTextField();
作为测试,但我希望使用
setTextField();
functions. 职能。 I do not have a “/Loader/Autoloader.php” as referenced in some guides.
我没有某些指南中提到的“ /Loader/Autoloader.php”。
How do I properly load the library so that I can use the setTextField();
如何正确加载库,以便可以使用
setTextField();
function? 功能?
PHP 5.4.16, Zend Framework 2.4.4, CentOS7 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. 首先,您上面提到的
Zend_Pdf
属于ZF1,而不是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). 如果您真的在谈论ZF2,则该类称为
ZendPdf
,可以用作独立组件-您不需要具有可用于自动加载的ZF2的完整副本(编写者将生成一个自动加载器-您只需要在您的脚本中require
)。 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. 上次我检查(承认是几年前)时,这两个版本在功能上是等效的,因此您可能应该只使用与实际使用的Zend Framework版本相匹配的版本。
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. 因为我不确定您要指的是哪个版本,所以进行了快速文本搜索,发现
setTextField()
方法仅存在于ZF1的Zend_Pdf
,而不存在于与ZendPdf
相关的ZendPdf
类中,因此我没有确定为什么在问题中提到ZF2。 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. 但是无论如何,我在发现之前就想出了如何使ZF2版本工作,因此为了完整起见,我在下面包括了这两种方法。
Also, you have an error in your require_once
statement - it should not have the '.:' included at the start. 另外,您的
require_once
语句中有一个错误-开头不应包含“。:”。 Now on to my actual answer. 现在到我的实际答案。
Loading Zend_Pdf
from Zend Framework 1 Standalone 从Zend Framework 1 Standalone加载
Zend_Pdf
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. 很显然,您需要调整上面代码中的路径以使其正常运行,但是一旦您执行了该步骤并运行了脚本,就应该最终创建example.pdf。
Loading ZendPdf
from Zend Framework 2 Standalone 从Zend Framework 2 Standalone加载
ZendPdf
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. 按照项目页面上的说明从github( https://github.com/zendframework/ZendPdf )下载
ZendPdf
项目,或者根据需要直接使用git
克隆存储库。
Change into the directory where ZendPdf
has been placed and run composer install
to download the supporting files. ZendPdf
放置ZendPdf
的目录,然后运行composer install
下载支持文件。
In that same directory (ie. the project root of ZendPdf
), create a file called test.php
with the following contents: 在同一目录(即
ZendPdf
的项目根目录)中,创建一个名为test.php
的文件,其中包含以下内容:
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. 运行
php test.php
并创建example.pdf文件。
The magic in that second solution comes from composer
, which creates the autoload.php
file for you. 第二种解决方案的魔力来自
composer
,它为您创建了autoload.php
文件。 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. 使用这种方法有很多很多好处,其中之一就是您不必为了获得有效的自动加载器而安装完整的Zend Framework 2副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.