Main Content

Fpdf - generating .pdf documents easily from your PHP program

Archive - Originally posted on "The Horse's Mouth" - 2012-07-24 22:38:09 - Graham Ellis

You can generate .pdf files from within a PHP page using the fpdf module which you can download from fpdf.org. It's quick to grab, easy to set up (pure PHP), and free of charge / royalty. Until today's PHP Techniques course, it was just one of thousands of modules that I know are out there, but I've not had a chance to try. But a customer requirement today (specifically a discussion about a use of instance variables - another story) lead us to download and try the class.

In Summary - use is as easy as
1. Load the class
  require('fpdf.php');

2. Create a new FPDF object
  $pdf = new FPDF();

3. Add a page to the output document
  $pdf-> AddPage();

4. Set the font and font size
  $pdf-> SetFont('Arial', 'B',16);

5. Add your text
  $pdf-> Cell(80, 8,'07:00 - 09:00 - Breakfast',0,1,"C");

6. Send the pdf to the browser
  $pdf-> Output();

Clearly there are lots of repeats of these lines to create the sample I have shown above, and I've used setmargin, setx, sety and settextcolor too ... full code [here].

Pre-requisite to using classes such as these:
a) Some knowledge about the take they'll undertake (you must be able to answer "What is a .pdf"?)
b) Knowledge of Object Orientation and how it works in PHP - see [here] for the appropriate course if you know PHP, but "OO" is new to you!