Easy PDF Generation in PHP
By Akash Mehta
2008-03-01
Examining the code
Let's take a look through that Hello World! snippet we just wrote.
<?php
require('fpdf.php');
$PDF = new FPDF();
$PDF->AddPage();
$PDF->SetFont('Arial', 'B', 16);
$PDF->Cell(40, 10, 'Hello World!');
$PDF->Output();
Barring the output, this is pretty standard stuff. Here's a quick rundown.
We start by including the FPDF library with the require('fpdf.php')
line. FPDF is pure PHP: that is, no PECL extension is required. It's a
little slower than PDFLib and CPDFLib, but it's infinitely more
portable and much easier to use.
We create a new instance of the FPDF class in $PDF.
Typically, for each PDF document you want to work with (each document
can have multiple pages), you'll want to use a new object. Chances are,
however, that you'll only ever need one.
This is where we launch into the actual PDF construction. We first
need to create a page. That's right - you can have a PDF document with
no pages. If you omit the AddPage() call and the
subsequent two lines, then try it in your browser, Adobe Reader will
display a blank empty page, but there really isn't one. As an example,
let's take a look at what the library outputs without any pages, fonts
or text cells created:
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 19>>
stream
x�3R��2�35W(�
Nothing terribly spectacular, especially consider its an empty document.
However, this is a barebones PDF document, and while you may never have
to actually work with this, it's important to be aware of what's
happening. As this is all purely text, all the Output() method (which we'll examine in a moment) does is send a HTTP header of application/pdf and print this data to the page.
But back to our code. Here's the bulk of our PDF:
$PDF->AddPage();
$PDF->SetFont('Arial', 'I', 14);
$PDF->Cell(40, 10, 'Hello World!');
This tells FPDF to first create a page within our PDF document (it
works with an internal copy while constructing your PDF), then set the
font currently in use to italicised Arial at 14pt. If you don't set a
font, it will default to no font and your text will not display as part
of the PDF.
Finally, we create a text cell on the page, with a width of 40 and height of 10, and write the text 'Hello World!' in it. The Cell()
method is the workhorse of the FPDF library. A cell is a rectangular
area with optional borders, background colour and character string.
Whenever you want to print text to your PDF file, chances are you'll be
using the Cell() method. The method has eight arguments,
even though we've only supplied three here. The others include a border
option, text alignment, transparent or filled background and URL - read
this manual page on the Cell method for further information.
The Output() method is especially interesting. By
default it will output the PDF file as the current web page (or shell),
but it can also save your newly generated PDF file to disk. To save to
disk, for example, we would call the method with $PDF->Output('output.pdf', 'F').
In the first argument, we specify the file name to output (typically
relative to the directory of the current script, not the FPDF library
file). In the second, we specify 'F' for File, to save to a local file.
Other options include sending inline to the browser (default), forcing
the browser to download the file, or even simply returning the document
data as a string. See this manual page for more details on the options available.
Tutorial Pages:
»
Introduction
»
Why would you want to generate PDFs?
»
Options for PDF Generation
»
Hello World with FPDF
» Examining the code
»
Further reading on FPDF