How to use the PDF document function in php

Source: Internet
Author: User
Tags pdflib
How to use the PDF document function in php, read how to use the PDF document function in php, and write it to Hunte: I haven't seen you on the Internet for a long time. I can't tell you that, without hunte is nothing. I turned to the two articles on your website and turned over one to apologize. --------------------------------------------- to Hunte:
I haven't seen you on the Internet for a long time. I can't say anything. I didn't know hunte's I turned to the two articles on your website and turned over one to apologize.

Original Author: Perugini Luca (
Http:// (znsoft)
Reprinted please keep the above information; otherwise, please do not reprinted!

PHP bundling the PDFLIB library may be the best web publishing platform. Typical usage:

Demand brochure
E-Commerce waybill

With this guide, you can learn how to use the PDF extension in php4 to create PDF documents.
We also focus on using mysql data to create PDF documents.

Content summary

Install PDFLib 3.0.1 and PHP4.01pl2 supported by PDF)

Extract PDF documents
(I suppose you have a little experience configuring php)

Install PDFLib and PHP supported by PDF.


PHP 4.02 + Download from
PDFLib 3.0.1 download from

This is a secret recipe for working with PDFLib3.0.1 and php4: (humorous foreigner ^_^)

Download the ext/pdf. c patch from the directly to support PDFLib v 3.0.1

Download PDFLib3.0.1 from here
Applicable patches you can find here

Configure, Make, and install PDFLib

#./Configure -- enabled-shared-pdflib
# Make
# Make install
You will install PDFLib in/usr/local/lib.

Configure 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

Update system Library
Insert/usr/local/lib into/etc/ld. so. conf (file)


Test and verification
Now you need to restart apache
# Apachectl restart

Copy javasclock. php to the httpd Directory (web directory)... test... everything works.

Important Information

To make PHPLIb and font work together, you must pay attention to the UPR section in the PDFLib manual.
The simplest way to use PDFLib to use fonts is to copy the standard UPR description file (fonts/PDFlib. upr) in the pdflib tar package to your working directory.

Extract PDF documents
Now we have made the conditions for generating PDF documents like flying!

In this small example, we will generate a FLYStore requirement booklet, which of course extracts data from the directory database.

Prepare database
I assume that you have a little experience with databases. to the minimum extent, I really only want you to know how to create a database and insert tables into it.
Create a table 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 (20 ))

Send MIME header information
In order for us to display the information correctly, we need to send the correct header information to the user's browser.
In PHP, we can use the header function. The following code sends the correct MIME type to the browser.

Header ("Content-type: application/pdf ");
Header ("Content-Disposition: attachment; filename=moduloment ");
Header ("Content-Description: PHP3 Generated Data ");

Important Information
You must know that you cannot output anything before sending the header information. A common error is that there is a space at the beginning of the file.

Retrieve data from mysql

Here we use a simple code snippet to extract data from the directory data.

$ Link = mysql_connect ("", "flyadm", "flystore ")
Or die ("cocould 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 );


Generate PDF file

To generate a PDF document, follow these steps:

Open a PDF stream and associate it with a handle:
$ Pdf = pai_open ();

(Optional) Set document information like Author, Title, Subject, etc
(Optional) set document information, such as author, title, topic, etc.

Start a new page (PDF files can be used to generate different pages, such as portraits, foreground ...):
__Begin_page ($ pdf, 595,842 );
(Optional) set a hyperlink:
Pai_add_outline ($ pdf, "Item". $ data [1]);

Select the font type and size (pdf_set_font ($ pdf, "Helvetica-Bold", 20, winansi );).

Insert text at X. y:
Pai_show_xy ($ pdf, "Item:". $ data [1], 100,700 );

Or insert an image at the X.Y position:
Pai_place_image ($ pdf, $ im, 100,300, 3 );

Refresh the text buffer and close the PDF stream.
PDF Coordinate Systems
What we need to do to locate a string or picture in some part of the PDF page,
In many places on the PDF page, we need to locate the string and image and convert the imperial unit to the DTP point value.

On page 45 of the PDFLIB manual, write:

"... The default coordinate system origin is located in the lower left corner of the page, and the unit is DTP:
1 pt = 1 inch/72 = 25.4/72 = 0.3528

Here is the code snippet for generating a PDF file:

$ Pdf = pai_open ();
__Set_info_author ($ pdf, "Luca Perugini ");
Pai_set_info_title ($ pdf, "Brochure for FlyStore ");
Pai_set_info_creator ($ pdf, "See Author ");
Pai_set_info_subject ($ pdf, "FlyStore ");
__Begin_page ($ pdf, 595,842 );
Pai_add_outline ($ pdf, "Item". $ data [1]);
__Set_font ($ pdf, "Helvetica-Bold", 20, winansi );
Pai_set_text_rendering ($ pdf, 0 );
Pai_show_xy ($ pdf, "FlyStore Catalogue 2000", 50,780 );

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

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

$ Im = pai_open_jpeg ($ pdf, "pass4_sml.jpg ");
Pai_place_image ($ pdf, $ im, 100,300, 3 );
Pai_close_image ($ im );

Performance_stroke ($ pdf );
Pai_end_page ($ pdf );
Pai_close ($ pdf );


At the end, I want to remind you that this article is not a PDF tutorial. if you need more information and usage of the PDF document, you can visit
Http:// and /.

I hope it will be useful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.