Dynamically generated documents are almost required in modern web applications as an easy way to send content to all – from CSV and excel CRM data to graphs, reports and invoices. As the de facto electronic document standard Adobes PDF is well in the lead in easily distributable content. Unfortunately, creating pdf files is quite a difficult task if you do not have access to the server configuration to install a component or library (such as PDflib) – unless you download the quite excellent FPDF Class.
FPDF is the most tested method to create PDFs without using libraries, and is very easy to use to boot. The one drawback is obviously a slight performance decrease – though kept at very manageable levels if your pdf isn’t too complicated. For high performance pdf generation you’re almost definitely going to need PDFlib – but you probably won’t be working on a shared host anyway!
To start off, it can’t get much easier than this:
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
Really simple! You’ll be knocking out cells before you know it and there are many more features including headers, footers, tables(ish), images, fonts/colours/lines. This is how easy it is to add a header and footer, simply extend the fpdf class!
class PDF extends FPDF { //Page header function Header() { $this->Image('howarths-logo.png',50,10,50); $this->SetFont('Arial','B',15); $this->Cell(70); $this->Cell(20,20,'Title'); $this->Ln(10); } }$pdf=new PDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output();
Notice the new class, extended from fpdf, is used to create the new pdf. The pages are added as before with the new header at the top of each of them! Much more can be found in the documentation here.
You can view a dynamically generated example from a brand new We Love creation, howarths.nl, here: Howarths product list
Happy pdf-ing!