How to use PDF document in PHP function _php Foundation

Source: Internet
Author: User
Tags pdflib
Write to Hunte:
For a long time not to see you online, really a bit can not say the feeling, no Hunte phpuser.com nothing. Turn the two articles you stand on, special turned over an apology.

----------------------------------------------------
Original Author: Perugini Luca (www.phpbuilder.com)
Translator: Znsoft (http://www.phpease.com)
---------------------------------------------------
Reprint please keep the above information, otherwise please do not reprint!

The PHP bundled Pdflib library may be the best Web publishing platform. A pair of typical usage:

Demand Brochure
E-commerce Shipping list

With this guide, you can learn how to use the PDF extensions 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 with PDF support: You can install the latest PHP4.03PL1

Extract PDF document
(I suppose you have a little experience in configuring PHP)



Install Pdflib and PHP with PDF support.

Demand:

PHP 4.02+ Download from http://php.net
Pdflib 3.0.1 Download from http://www.pdflib.com

This is how to let PDFLib3.0.1 and PHP4 work together a small secret recipe: (Foreigner very humorous ^_^)

Download the EXT/PDF/PDF.C patch directly from http://www.php.net to support Pdflib v 3.0.1

Download PDFLib3.0.1 from here http://www.pdflib.com
Applicable patches You can find http://www.pdflib.com/pdflib/patches.html here

Configure, make, and install Pdflib

#./configure--enabled-shared-pdflib
#make
#make Install
You will make Pdflib installed 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)

#/sbin/ldconfig



Testing and validation
Now you need to restart Apache.
#apachectl restart


Copy pdfclock.php to the httpd directory (that is, the Web directory) ... Test.... Everything's fine.

Important Information

To make phplib work with fonts you must pay attention to the UPR part of the Pdflib manual.
The simplest way to use Pdflib fonts is to copy the standard UPR description file (FONTS/PDFLIB.UPR) in the Pdflib tar package to your working directory.

Extract PDF document
Now we have the conditions to generate the PDF document as quickly as we could!


In this small example we want to generate the Flystore company's demand brochure, of course, to extract data from the catalog database.




Preparing the Database
I assume you have a bit of database experience, minimal, and I really just want you to know how to create a database and insert a table into it.
CREATE TABLE Catalogue:

CREATE TABLE Catalogue (
ID smallint (8) unsigned DEFAULT ' 0 ' not NULL,
Item varchar 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))
);



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

Header ("Content-type:application/pdf");
Header ("content-disposition:attachment; Filename=modulo.pdf ");
Header ("Content-description:php3 generated Data");



Important Information
What you have to know is that you can't output anything until you send a message. A common error is that there are spaces at the beginning of the file.


Take count from MySQL

Here we use a simple snippet of code to extract data from the catalog data.
<?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);


?>


Generate PDF file

In order to generate the PDF document, we need to do the following steps:


Open a PDF stream and associate it with a handle:
$pdf = Pdf_open ();

(Optional) Set Document information like Author, Title, Subject, etc
(optional) Set up document information, such as author, title, subject, etc.

Start a new page (PDF file can produce different pages in different layouts, such as portrait, foreground ...):
Pdf_begin_page ($pdf, 595, 842);
(optional) Set up a hyperlink:
Pdf_add_outline ($pdf, "Item". $data [1]);

Select font type, size (Pdf_set_font ($pdf, "Helvetica-bold", Winansi);) Performance mode

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

or insert picture in x.y position:
Pdf_place_image ($pdf, $im, 100, 300, 3);

Refreshes the text buffer and closes the PDF stream.
PDF coordinate Systems
What We need to be locate a string or picture in some part of the PDF page,
In many places on the PDF page we need to locate strings and pictures, convert English units to DTP point values.

In the Pdflib manual, page 45 reads:



".. . The origin of the default coordinate system is in the lower left corner of the page, with the DTP point as the unit:
1 pt = 1 INCH/72 = 25.4MM/72 = 0.3528 mm
"


Here is the code snippet that generated the PDF file:
<?php

$pdf = Pdf_open ();
Pdf_set_info_author ($pdf, "Luca Perugini");
Pdf_set_info_title ($pdf, "brochure for Flystore");
Pdf_set_info_creator ($pdf, "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", 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);


?>

In the end, I want to prompt you this article is not a PDF tutorial, if you need more information and usage of PDF documents, you can access
http://www.pdfzone.com/and http://www.planetpdf.com/.

I hope it works for 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: info-contact@alibabacloud.com 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.