One of the best known issues in web design is the difficulty to translate a page as seen on the screen, rendered by your preferred browser, into an actual printed page, as generated by a common paper printer. I can’t remember how many times I saw a graphics designer plan a web page using centimeters, page formats (A4 is the most diffused, of course) and layout cages based on his experience on paper projects; it seems that very few peoples understand that this particular media needs its own tools to deal with…
A wise designer always give users the possibility to access a print-ready version of the current document, in order to generate an acceptable print output; this is usually carried out through dedicated CSSs, which can drive different media (video, printer, and so on), everyone with its own setup. So, it’s possible to rearrange in real-time a web document to be printed by hiding useless parts of the page, such as sidebars, banners, commercial ads, headers, or by providing elements customized for printing purposes, like text-only footers. You can try yourself with this very article, by clicking on the “Print this article” feature at the top of this page, and see what happens when a dinamic content is provided with a CSS container customized for printed outputs: what you get is a “clean” version of the document, with very little graphics and an opportune formatting of the text contents.
But in the vast ocean of possibilities offered by computer programming, there is another way to give users the opportunity to retrieve a printable version of the document visible on the screen: let the system compose in real-time a PDF version to be downloaded and printed on demand. In this article, we’ll see a nice and easy way to embed on-the-fly PDF generation in your personal PHP application.
For this purpose there are many different ways to achieve PDF generation through PHP, perhaps the most known procedure is to take advantage of the PDFlib library, the leading developer toolbox for generating and manipulating files in Adobe’s well known Portable Document Format. PDFlib is not free for professional purposes, even if PDFlib Lite, a subset of PDFlib’s functionality, can be used for free under certain conditions.
In this short tutorial about PDF procedural generation, we’ll deal with a smaller, free alternative: FPDF.
FPDF is distributed as a PHP class which allows user to compose PDF files merely through PHP, with no needs to access the PDFlib library. FPDF offers high level functions, and many useful features:
- custom units, page formats and margins
- custom page header and footer
- automatic page break
- automatic line break and text justification
- direct support for images management (both JPEG and PNG)
- colors
- links embedded
- TrueType, Type1 font and page encoding support
- page compression
The official web site distributes also a collection of free PHP scripts, allowing users to quick start their experience with actual and useful examples, to be reengineered or embedded in larger custom projects.
In order to be operative, FPDF requires no extensions (except zlib to activate compression) and works flawlessy with both PHP4 and PHP5. Overall efficience and speed is lower than in PDFlib (as clearly said in the FPDF docs), but for real world users this penalty is not visible at all.
Ok, let’s go to the point, and take a look at a very first usage; in this article, we presume that you’re an experienced PHP user (even with very little experience, don’t worry!), with a personal PHP development environment available (often a simple web site in a shared hosting environment), better if a Linux-based one, even if it’s possible to work with PHP under Windows too (if you like such kind of things)…
The very first steps to be taken are quite straightforward:
- include the FPDF library into your PHP document;
- create a new PDF object;
- customize object’s methods to design the PDF document
Let’s see a code snippet corresponding to this recipe:
require('fpdf');
$pdf_doc=new FPDF();
$pdf_doc->AddPage();
$pdf_doc->SetFont('Arial','B',32);
$pdf_doc->Cell(60,20,'Cheers from DeepVoid!');
$pdf_doc->Output();
The meaning of these lines is quite clear: the FPDF() constructor is used without arguments (that is, the defaults are used: A4 format in portrait orientation, measure units in millimeters); the function AddPage() is invoked to create a new blank page within the PDF document; the function SetFont() with its own parameters allows to set the characters to be used; the function Cell() is used to design a text box with two parameters (width and heigth) and its textual content. At the end, the function Output() is used to generate the actual PDF file, that could be passed to the browser for the user to download it via http.
You can go further by exploring the official web site, which sports advanced tutorials and interesting case samples to analyze in depth the features of FPDF.
Have a nice coding time!
I have never heard of FPDF before, and this is an excellent tutorial, and the code if very easy to execute
How we use itext with java for pdf and kind of standard report generation.?