Home php教程 php手册 怎样在php中使用PDF文档功能

怎样在php中使用PDF文档功能

Jun 13, 2016 pm 12:37 PM
pdf php use Function exist how document of

写给Hunte:
好久不在网上见你,真有点说不出来的感觉,没有hunte的phpuser.com什么也不是. 转了你站上的两篇文章, 特翻了一篇了以示致歉.

----------------------------------------------------
原作者:Perugini Luca (www.phpbuilder.com)
译者:znsoft (http://www.phpease.com)
---------------------------------------------------
转载请保留以上信息,否则请不要转载!

PHP捆绑PDFLIB库也许是最好的web出版平台了。一对典型的用法:

需求小册子
电子商务发货单

通过这个指南,你可以学会怎样使用php4中的PDF扩展来创建PDF文档。
我们也把焦点放在用mysql数据来创建PDF文档。

内容摘要


安装PDFLib 3.0.1 和有PDF支持的PHP4.01pl2(译注:你可以安装最新的php4.03pl1)

提取PDF文档
(我假设你有一点配置php的经验)



安装PDFLib和有PDF支持的PHP。

需求:

PHP 4.02+ 从 http://php.net 下载
PDFLib 3.0.1 从 http://www.pdflib.com 下载

这是一个怎样让PDFLib3.0.1和php4一起工作的小秘方:(老外很幽默的^_^)

直接从http://www.php.net下载 ext/pdf/pdf.c的补丁来支持PDFLib v 3.0.1

下载PDFLib3.0.1从此处 http://www.pdflib.com
适用的补丁你可以在此找到 http://www.pdflib.com/pdflib/patches.html

配置,Make和安装PDFLib

#./configure --enabled-shared-pdflib
#make
#make install
你会使得 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



更新系统库
插入 /usr/local/lib 进 /etc/ld.so.conf (文件)

#/sbin/ldconfig



测试和验证
现在你需要重启apache
#apachectl restart


拷贝pdfclock.php 到的httpd目录中(就是web目录)...测试....一切正常。

重要信息

要使得PHPLIb和字体一起工作你必须注意PDFLib手册中的UPR部分。
最简单的用PDFLib使用字体的办法是拷贝PDFlib tar包中的标准UPR描述文件(fonts/pdflib.upr)到你的工作目录。

提取PDF文档
现在我们已经作好了如飞地生成PDF文档的条件!


在这个小例子中我们要生成FLYStore公司的需求小册子,当然是从目录数据库中提取数据。




准备数据库
我假设你有一点数据库的经验,最小限度,我真的只希望你懂得怎样创建一个数据库并向其中插入表。
创建表 catalogue:

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: PHP3 Generated Data" );



重要信息
你必须知道的是在送出头信息前不能输出任何东西。一个常见的错误是在文件的开头存在空格。


从mysql中取数

这儿我们用一个从目录数据中提数据的简单代码片断。

$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);


?>


生成PDF文件

为了生成PDF文档,我们需要作经过以下步骤:


打开一个PDF流,并使它和一个句柄关联:
$pdf = PDF_open();

(Optional) Set document information like Author, Title, Subject, etc
(可选的)设置文档信息,如作者,标题,主题,等

开始一个新页(PDF文件可以用不同的版面生成不同的页,比如肖像,前景...):
PDF_begin_page($pdf, 595, 842);
(可选的)设置一个超链接:
PDF_add_outline($pdf, "Item ".$data[1]);

选择字体类型, 尺寸 (pdf_set_font($pdf, "Helvetica-Bold" , 20, winansi);) 表现模式

插入文本在X.y位置:
PDF_show_xy($pdf, "Item : " .$data[1], 100, 700);

或插入图片在X.Y位置:
pdf_place_image($pdf, $im, 100, 300, 3);

刷新文本缓冲区并关闭PDF流。
PDF Coordinate Systems
What we need to do to locate a string or picture in some part of the PDF page,
在PDF页的很多地方我们需要定位字符串和图片,转换英制单位到DTP点值.

在PDFLIB手册的45页写到:



".. .缺省的坐标系统的原点在页面的左下角,用DTP点作为单位:
1 pt = 1 inch /72 = 25.4mm /72 = 0.3528 mm
"


这儿是生成PDF文件的代码片断:

$pdf = PDF_open();
pdf_set_info_author($pdf, "Luca Perugini");
PDF_set_info_title($pdf, "Brochure for FlyStore");
pdf_set_info_creator($pdf, "See Author");
pdf_set_info_subject($pdf, "FlyStore");
PDF_begin_page($pdf, 595, 842);
PDF_add_outline($pdf, "Item ".$data[1]);
pdf_set_font($pdf, "Helvetica-Bold" , 20, winansi);
pdf_set_text_rendering($pdf, 0);
pdf_show_xy($pdf, "FlyStore Catalogue 2000",50,780);

PDF_show_xy($pdf, "Item : " .$data[1], 100, 700);

PDF_show_xy($pdf, "Description : " .$data[2], 100, 620);

$im = PDF_open_jpeg($pdf, "pass4_sml.jpg");
pdf_place_image($pdf, $im, 100, 300, 3);
pdf_close_image ($im);

pdf_stroke($pdf);
PDF_end_page($pdf);
PDF_close($pdf);


?>

在最后,我要提示你这篇文章不是PDF教程,如果你需要更多的PDF文档的信息和用法,你可以访问
http://www.pdfzone.com/ 和 http://www.planetpdf.com/.

我希望对你有用。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles