Purple pattern background

Creating PDF's on the fly using PHP & FPDF

Mohammed NaserMohammed Naser

Generating PDF's is an easy method to make a very nice printable and/or savable version of an article. This could be helpful in a WordPress blog or any articles website. This method utilizes the popular FPDF class.

First of all, I suggest you get the latest FPDF version. I suggest creating a directory such as “pdf” on your web hosting space. You will need to place the fpdf.php into that directory, you will only need it.

Let's make our first “Hello World” example. Place the following in a file called test.php:

,


<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->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 our FPDF class that we need to create the 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',

<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  

6); ````
This line tells the FPDF class to change the font to Arial, bold, ,

<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  

6 pt.

$pdf->Cell(4

  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  
```  
  
,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,'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 4,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 pt. and a height of ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  
```  
  
,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 pt., the third value is the text to be written in that rectangular box.   
  
`$pdf->Output();`    
Our final line, which pretty much means: “we're done, show us our PDF!”  
  
That's a very simple FPDF usage, however, here is a bit of more advanced version of FPDF usage:  
  
,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  
```  
  
\>  
  
Now let's do a quick review of that more advanced code:  
  
`$pdf=new FPDF("L", "mm", "A4");`    
This will create a new instance but instead will default to creating landscape pages because of the first `L`, `P` can be used instead to default to portrait pages. The second value is the default measurement unit, a choice of point (`pt`), millimeter (`mm`), centimeter (`cm`) and inch (`in`) is given. The last value is the size of the page, the choice of `A3`, `A4`, `A5`, `Letter` & `Legal` is given.  
  
`$pdf->AddPage();`    
Simple function, just add the page, you can tell the function to create either a portrait (`P`) or landscape (`L`) by giving it as a first value (ex: `$pdf->AddPage("L")`, `$pdf->AddPage("P")`).  
  
`$pdf->SetFont('Arial','BIU',38);`    
This required function again says that we want an Arial, 3  
  
,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 mm in size (because of the default size unit), the `'BIU'` simply tells that we want it to be **B**old, **I**talic & **U**nderlined.   
  
`$pdf->SetTextColor(`  
  
```` ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,255); ````    
This sets the default text color for the text we will be writing, I've chosen blue here, the first value is red (`r`), the second is green (`g`) & blue (`b`).   
  
`$pdf->Cell(6`  
  
```` ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,2,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,'PDF+PHP Test',,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
,,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  
```  
  
,C,,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
); ````    
This makes the so famous rectangle with 6,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 mm of width & 2,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 mm of height, we wrote 'PDF+PHP Test' and the first ```` ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 ```` means we do not want a border. The ```` ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF("L", "mm", "A4");    
$pdf->AddPage();    
$pdf->SetFont('Arial','BIU',30);    
$pdf->SetTextColor(0,0,255);    
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);    
$pdf->Output();    
?>  
  
```  
  
 ```` next to it means that once it's done the cell, it will go to the beginning of the next line, if ```` ,  
  
```  
<?php    
require('fpdf.php');    
$pdf = new FPDF();    
$pdf->AddPage();    
$pdf->SetFont('Arial','B',16);    
$pdf->Cell(40,10,'Hello World!');    
$pdf->Output();    
?>  
  
```  
  
 ```` is provided, then it will be to the right of it, if `2` is provided then it will go below. The `C` is just the alignment which is center of the text inside the box, possible values are left (`L`), center (`C`), right (`R`).   
  
`$pdf->Output();`    
Output our brand new colorful PDF file!  
  
Thanks for reading!

Share on social media

Virtual machines, Kubernetes & Bare Metal Infrastructure

Choose from Atmosphere Cloud, Hosted, or On-Premise.
Simplify your cloud operations with our intuitive dashboard.
Run it yourself, tap our expert support, or opt for full remote operations.
Leverage Terraform, Ansible or APIs directly powered by OpenStack & Kubernetes