SoFunction
Updated on 2025-04-08

How to use PDF document function in php

Written to Hunte:
I haven't seen you online for a long time. I feel like I can't say anything. I'm not hunting. I turned on two articles on your site and read one to apologize.

----------------------------------------------------
Original author: Perugini Luca ()
Translator:znsoft ()
---------------------------------------------------
Please keep the above information when reprinting, otherwise please do not reprint!

PHP bundled PDFLIB library is perhaps the best web publishing platform. A typical usage of a pair:

Needs brochure
E-commerce delivery order

With this guide, you can learn how to create PDF documents using PDF extensions in php4.
We also focus on creating PDF documents with mysql data.

Content Summary


Install PDFLib 3.0.1 and PHP4.01pl2 with PDF support (translator's note: you can install the latest php4.03pl1)

Extract PDF documents
(I assume you have some experience in configuring php)



Install PDFLib and PHP with PDF support.

need:

PHP 4.02+ Download from
PDFLib 3.0.1 Download from

This is a little secret recipe for how to make PDFLib3.0.1 and php4 work together: (Foreigners are very humorous^_^)

Support PDFLib v 3.0.1 directly from the patch downloaded ext/pdf/

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

Configure, Make and Install PDFLib

#./configure --enabled-shared-pdflib
#make
#make install
You will make PDFLib install 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 the system library
Insert /usr/local/lib into /etc/ (file)

#/sbin/ldconfig



Testing and Verification
Now you need to restart apache
#apachectl restart


Copy the httpd directory (that is the web directory)........ Everything is normal.

Important information

To make PHPLIb work with fonts you must pay attention to the UPR section in the PDFLib manual.
The easiest way to use fonts with PDFLib is to copy the standard UPR description file (fonts/) in the PDFlib tar package to your working directory.

Extract PDF documents
Now we have made the conditions for generating PDF documents as enclaves!


In this small example, we want to generate the requirements brochure of FLYStore, which of course extracts data from the directory database.




Prepare the database
I'm assuming you have a little experience with databases, to a minimum, I really just hope you know how to create a database and insert tables into it.
Create 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 (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 it. The following code sends the correct MIME type to the browser.

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



Important information
What you have to know is that you can't output anything before sending the header message. A common mistake is the existence of spaces at the beginning of a file.


Take numbers from mysql

Here we use a simple code snippet of extracting data from directory 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 files

In order to generate a PDF document, we need to go through 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 document information, such as author, title, topic, etc.

Start a new page (PDF file can use different layouts to generate different pages, such as portraits, 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" , 20, winansi);) Performance mode

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

Or insert the image in location:
pdf_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 PDF pages, we need to locate strings and pictures, and convert the imperial units to the DTP point value.

On page 45 of the PDFLIB manual, write:



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


Here is the code snippet that generates 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, "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);


?>

At the end, I want to remind you that this article is not a PDF tutorial. If you need more information and usage of PDF documents, you can access
/ and /.

I hope it will work for you.