Using PHP to generate PDF document features _php tutorial

Source: Internet
Author: User
Tags mysql tutorial pdflib
Generate PDF document functionality with PHP

PHP bundled Pdflib library may be the best Web publishing platform. A couple of typical uses:

Demand Brochure
E-commerce Delivery order

With this guide, you can learn how to use the PDF extension in PHP Tutorial 4 to create PDF documents.
We also focused on using MySQL tutorial data to create PDF documents.

Content Summary


Install Pdflib 3.0.1 and PDF-supported PHP4.01PL2 (you can install the latest PHP4.03PL1)

Extracting PDF documents
(I assume you have a bit of 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 a small recipe for how to make PDFLib3.0.1 and PHP4 work together: (Foreigner very humorous ^_^)

Download 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 here http://www.pdflib.com/pdflib/patches.html

Configuration, 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



Updating the 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 (which is the web directory) ... Test.... Everything is fine.

Important Information

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

Extracting PDF documents
Now we've made the conditions for a PDF document to be generated quickly!


In this small example we are going to generate the Flystore company's demand booklet, which is, of course, extracting data from the Catalog database tutorial.




Prepare the database
I assume you have a little bit of database experience, and I really just want you to know how to create a database and insert a table into it.
To create a 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 ' is not NULL,
PRIMARY KEY (ID),
KEY Item (item (20))
);



Send out 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. The following code sends out 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 must know is that you cannot output anything until you send your message. A common mistake is that there is a space at the beginning of the file.


Take the number from MySQL

Here we use a simple snippet of code that extracts data from catalog data.

$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 files

In order to generate PDF documents, we need to take 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 be used to create different pages in different layouts, such as portrait, foreground ...):
Pdf_begin_page ($pdf, 595, 842);
(optional) Set a hyperlink:
Pdf_add_outline ($pdf, "Item". $data [1]);

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

Insert text in x.y position:
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 does to locate a string or picture in some part of the PDF page,
In many parts of the PDF page we need to position strings and images, converting imperial units to DTP point values.

On page 45 of the Pdflib Manual, write:



".. . The default coordinate system's origin is in the lower-left corner of the page, using a 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:

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


?>


http://www.bkjia.com/PHPjc/444931.html www.bkjia.com true http://www.bkjia.com/PHPjc/444931.html techarticle using PHP to generate PDF documents the PHP bundle Pdflib library may be the best Web publishing platform. A couple of typical uses: Demand brochure e-commerce invoice through this guide, you can learn ...

  • Related Article

    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.