![]() Join Up! 96812 members and counting! |
|
|||
PHP for PDF
Luca Perugini
PHP tied to PDFLib is perhaps the best platform for Web Publishing.
A couple of typical uses:
With this Tutorial you'll learn how to use the PDF extension in PHP4 to create PDF documents.
We also put focus on creating PDF documents with data from MySQL. Summary of Contents
I am going to assume a *little* experience in configuring and installing PHP.
Installing PDFLib and PHP with PDF Support
Requirements
This is a little recipe to get PDFlib 3.0.1 working with PHP4:
Important Notes
To get working PDFLib with fonts you must pay attention to the UPR section of the PDFLib manual.
A simple way to use fonts with PDFLib is to copy the standard UPR description file included in PDFLib tarball, (fonts/pdflib.upr) into your working directory. Distilling PDF document
Now we're ready to generate PDF on the fly!
In this little example we'll generate brochure on demand for FlyStore Inc.,
taking data from a catalogue database.
Prepare Database Test
I am going to assume a *little* experience in databases. Having said that, I'm
really only hoping that you know how to create a database and insert tables
into that database.
Creating 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)) ); Sending MIME Header Information
In order to have our document show up correctly, we need to
send correct header information to the user's browser.
With PHP we can do that using the header function.
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 Notes
What you need to know is that you must not send anything before header output.
A common mistake is the presence of spaces at the beginning of the file.
Getting Data From MySQL
Here we've got a snapshot of code for retrieving data from the catalogue database.
Generating PDF File
In order to generate PDF document, we need to do these steps:
PDF Coordinate Systems
What we need to do to locate a string or picture in some part of the PDF page,
is to convert from metric/inch distance to
DTP point correspondent value. At page 45 of PDFLib Manual we read:
".. . The default coordinate system (or default user space in PDF lingo) has the origin in the lower left corner of the page, and uses the DTP point as unit: 1 pt = 1 inch / 72 = 25,4 mm / 72 = 0,3528 mm"
Here is a snapshot code for generating PDF file:
In the end, I'd like to remind you that this article is not a PDF Tutorial,
so if you need more information about the PDF language and its use, you
must have a look at http://www.pdfzone.com/
and http://www.planetpdf.com/.
Files Used in This Tutorial:
I hope this has been of use to you.
Ciao
-- Luca |