Generate PDF in PHP
FPDF is a small, simple and powerful library for handling PDF documents in PHP. In this article, I will walk you through to use FPDF and generate a PDF document. Download the library from the FPDF website. Its a simple library and easy to use. There will be a file named
fpdf.php inside the download bundle.
fpdf.php is the core API file.
To generate PDF files, you need to copy the fpdf.php from bundle and use it in your application. Along with that copy the font folder also and paste it inside your application.
Generating a simple PDF document is simple. Code to generate a basic PDF document:
<?php require('fpdf.php'); $pdf = new FPDF(); $pdf->SetAuthor("Joe"); $pdf->AddPage(); $pdf->SetFont('Helvetica','B',24); // print a cell $pdf->Cell(40,10,'Hello PHP PDF World!'); // save or send the document $pdf->Output(); ?>
First we should include the FPDF library file. Instantiate FPDF class and its instance represents a PDF document. Using that instance add a page and then set appropriate values. Then finally call the Output() method to generate the PDF document. It is as simple as that. You can see more about generating complex PDF documents and formatting them in the coming articles.