PHP PDF Generation using FPDF

Posted By : Vikas Verma | 31-Jul-2019

FPDF class of PHP which helps us to generate PDF files with PHP code and that is to say without using the PDFlib library. It is free to use and it's independent of any API keys. FPDF stands for Free PDF.


The main features of this class are:

Allows to setup page format and margins.
Allows to setup page header and footer.
It provides an automatic page break and a line break.
It supports images in various formats (JPEG, PNG, and GIF).
It allows us to setup Colors and Links.
It also supports encoding.
It also supports the Page compression feature and provides many other functions.

 

FPDF is extension independent except Zlib to enable compression and GD for GIF support and requires at least PHP 5.1 to run this. A section of the script is also there to support useful extensions to work such as bookmarks, rotations, tables, barcodes.


There are no limits in regarding the document size but still, there are some constraints, however:
- there's typically a maximum memory size allotted to PHP scripts.

 

For very huge documents, especially with images, the limit may be reached (the file being built-in memory).
The parameter is configured in the php.ini file.

- the utmost execution time allotted to scripts defaults to thirty seconds. This limit can, of course, be easily reached.
It can be configured by some changes in php.ini and may be changed dynamically with set_time_limit() function.

 

What languages can I use?
Central European, Cyrillic, Greek, Baltic, and Thai, provided you own TrueType or Type1 fonts with
the desired character set. It supports UTF-8.

 

Let's make our first"Hello World" example.

AddPage();  
$pdf->SetFont('Arial','B',16);  
$pdf->Cell(40,10,'Hello World!');  
$pdf->Output();  
?>

 

Now let’s see that code line by line,

 

require('fpdf.php');
This line includes the FPDF class that we need to create a PDF file.

 

$pdf=new FPDF();
This line creates a new instance of the FPDF class which will be binded to $pdf

 

$pdf->AddPage();
This line tells FPDF to add a new page to the PDF file obviously, we need one page so we will call this function once.

 

$pdf->SetFont('Arial','B',24);
This line tells the FPDF class to change the font to Arial, bold, 24 pt.

 

$pdf->Cell(30,15,'Hello World!');
This line is just like the “echo” of PHP, the text fields in PDF files are just sort of rectangles with text in them, so we want the width of 30 pt. and a height of 15 pt., the third value is the text to be written in that rectangular box.

 

$pdf->Output();
Our last line, which means: “we're all set and ready generate our PDF!”

 

About Author

Author Image
Vikas Verma

He is frontend developer. He is a learner by heart and has a passion and profile to adapt various technologies.

Request for Proposal

Name is required

Comment is required

Sending message..