Create a PDF document with the PDF extension in PHP4

Source: Internet
Author: User
Tags key mysql pdflib
Summary: PDF documents are often used in electronic books, brochures, and so on, can effectively prevent copying and piracy, in the PHP4, we can create a document in PDF format? The answer is: Use the PDF extension Library in PHP.

How do I configure a running environment that supports PDFs?

First, we need to install Pdflib 3.0.1 and PHP4.0.1PL2 to support PDFs.

Software Requirements:

PHP 4.02+ ( http://www.php.net )
Pdflib 3.0.1 ( http://www.pdflib.com )

Download PHP's Uwe Steinman patch directly from http://php.net ext/pdf/pdf.c to support Pdflib v 3.0.1
Download Pdflib 3.0.1 from http://www.pdflib.com .
Install each patch on the http://www.pdflib.com/pdflib/patches.html .

Configure, compile, and install Pdflib

#./configure--enabled-shared-pdflib
#make
#make Install

You will install Pdflib under the directory/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 in/etc/ld.so.conf
#/sbin/ldconfig

Test

To restart Apache:

#Apachectl restart

Copy pdfclock.php to your httpd directory, and then test.

How do I generate a PDF document?

In this case, we'll make a brochure that takes the catalog from the database.

Pre-database test

Create a table of contents

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 our documents to be displayed correctly, we need to send the correct header information to the browser. In PHP, we can use the header function to do the following code to send the correct MIME type to the browser.

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

Fetching data from MySQL

The following is a section of code that takes records from the directory database.

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

Create PDF file

In order to create a PDF document, you need the following steps:
Opens a PDF stream and binds a handle.
$pdf = Pdf_open ();

Optionally, set the document information, such as author, title, subject, and so on.

Start a new page (a PDF document can create different pages with different layouts, such as portrait, landscape ...). )。

Pdf_begin_page ($pdf, 595, 842);

Optionally, set up a hyperlink:
Pdf_add_outline ($pdf, "Item". $data [1]);

Select the font, size (Pdf_set_font ($pdf, "Helvetica-bold", Winansi), and render mode.

Insert text in x,y position:
Pdf_show_xy ($pdf, "Item:". $data [1], 100, 700);

Insert the image in the x,y position of the PDF document:
Pdf_place_image ($pdf, $im, 100, 300, 3);

Output a text buffer and close the PDF document.

How do I apply a PDF coordinate system?

We want to locate a string or picture in a part of a PDF page and ask us to move from the ruler/inch distance to the corresponding DTP point value. The following words are available on page 45 of the Pdflib manual:

"...... The default coordinate system (or the default user whitespace in PDF terms) is the origin in the lower-left corner of the page, and the DTP point is used as the unit:

1 pt = 1 INCH/72 = 25,4 mm/72 = 0,3528 mm

This is a section of code to create a 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, "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);
? >



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.