综述:PDF文档常被用在电子图书、说明书等方面,可以有效地防止拷贝与盗版,在PHP4中,我们能创建一个PDF格式的文档吗?答案是:运用PHP中的PDF扩展库。
如何配置支持PDF的运行环境?
首先,我们需要安装PDFLib 3.0.1 和 PHP4.0.1pl2 以支持PDF。
软件要求 :
PHP 4.02+ ( http://www.php.net ) PDFLib 3.0.1 ( http://www.pdflib.com )
直接从http://php.net下载PHP的Uwe Steinman 补丁 ext/pdf/pdf.c 以支持 PDFLib v 3.0.1 从http://www.pdflib.com 下载 PDFLib 3.0.1。 安装http://www.pdflib.com/pdflib/patches.html上的每一个补丁。
配置、编译及安装 PDFLib
你将安装 PDFLib 在目录 /usr/local/lib 下。
配置PHP
#./configure --with-apxs=/usr/bin/apxs --with-gd --with-pdflib=/usr/local --with-mysql=/usr/local --with-config-file-path=/etc/httpd --with-zlib-dir=/usr --with-ttf=/usr/local/include --with-jpeg-dir=/usr --with-tiff-dir=/usr --with-system-regex=yes --enable-debug=no #make #make install 更新系统库
在/etc/ld.so.conf 中插入 /usr/local/lib #/sbin/ldconfig
测试
要重启 Apache :
#Apachectl restart
拷贝pdfclock.php 到你的httpd目录,然后测试。
如何生成PDF文档?
本例中我们将制作一个小册子,从数据库中取到册子的目录。
预备的数据库测试
创建目录表
create table catalogue( id smallint(8) unsigned DEFAULT '0' NOT NULL, item varchar(100) DEFAULT '' NOT NULL, description tinytext, img_data longblob, imgname varchar(60), imgsize varchar(60), imgtype varchar(60), price smallint(8) unsigned DEFAULT '0' NOT NULL, PRIMARY KEY (id), KEY item (item(20)) ); 发送 MIME 头信息
为了让我们的文档能正确地显示,我们需要发送正确的头信息给浏览器。在PHP下,我们可以用header函数来做,以下代码发送了正确的MIME类型给浏览器。
header( "Content-type: application/pdf" ); header( "Content-Disposition: attachment; filename=modulo.pdf" ); header( "Content-Description: PHP Generated Data" ); 从MySQL取数据
以下就是一段从目录数据库中取记录的代码。
<?php $link = mysql_connect ("127.0.0.1", "flyadm", "flystore") or die ("Could not connect"); mysql_select_db ("flystore", $link); $result = mysql_query ("SELECT * FROM catalogue", $link) or die ("Invalid query"); $data = mysql_fetch_row ($result); …… …… mysql_close ($link); ?>
|