Creating PDF Files using PHP FPDF library

You can use FPDF library to easily create PDF file for your project. It's a free alternative to other commercial PDF generators. It comes with many features too, which you can use to create a wonderful looking PDF files. FPDF is not an PHP extension, just include the class file in your project and you are ready to go, it works with both PHP 4 and PHP 5.

FPDF Basics

Today let's learn to use it on our projects. Just download FPDF library and place the extracted file within your project folder. Create a PHP file with following code and execute just to see what happens.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
<?php require('fpdf17/fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?>
Let's find-out what this code really does. We basically start by including fpdf.php in our PHP file something like this :
PHP
  • 1
require('fpdf17/fpdf.php');
Now we need to create an FPDF object, we can just call FPDF() constructor with default values to do that, or we can also specify different values that controls the output of PDF file, such as orientation, unit and size.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
$pdf = new FPDF(); //default //OR with settings : FPDF('orientation','unit','size'); //Orientation -- P or Portrait & L or Landscape //Unit --pt: point, mm: millimeter, cm: centimeter, in: inch //Size -- A3, A4, A5, Letter, Legal $pdf = new FPDF('P','mm','A4'); //with page settings
And then the mandatory step is to include font size, in this example its Arial font with bold weight and 16 size.
PHP
  • 1
$pdf->SetFont('Arial','B',16);
We can now print a line of text with Cell() on PDF document. A cell is a rectangular area, possibly framed, which contains a line of text. It is output at the current position. We specify its dimensions, its text (centered or aligned), if borders should be drawn, and where the current position moves after it (to the right, below or to the beginning of the next line). To add a frame, we would do this:
PHP
  • 1
$pdf->Cell(40,10,'Hello World !',1);
To add a new cell next to it with centered text and go to the next line, we would do:
PHP
  • 1
$pdf->Cell(60,10,'Powered by FPDF.',0,1,'C');
Finally we send PDF output to the browser or file, below example outputs PDF to the browser, to save it as a file we just need to pass a filename.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
$pdf->Output(); //default output to browser //PDF output settings //I: to the browser. //D: force a file download //F: save to a local file //S: return the document as a string. name is ignored. $pdf->Output('sara.pdf', 'D'); //force download file

PDF with line breaks

So far we are able to create a PDF file, but above example just outputs a single line of text, what was missing here was the line breaks. We can easily achieve line breaks using MultiCell() method, all we need to specify is the width and height of the cell.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
<?php require('fpdf17/fpdf.php'); $the_content = "Ut sagittis erat vitae nunc viverra, ut bibendum dui sodales. In fermentum, augue vel vestibulum porttitor, lectus ipsum faucibus justo, tincidunt luctus velit odio quis orci. "; $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); //specify width and height of the cell Multicell(width, height, string) $pdf->Multicell(190,10,$the_content); $pdf->Output(); ?>

Create PDF from a URL

You can easily create PDF file from a URL, we need to play a bit with PHP DOM or you can use Simple HTML DOM Parser for that matter, have a look at the code below.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
<?php //include fpdf require('fpdf/fpdf.php'); //read the contents of a url you want to convert to PDF $content = file_get_contents('https://www.sanwebe.com/2011/05/generating-pdf-using-php-fpdf-library'); //Load PHP DOM $doc = new DOMDocument(); //load HTML in PHP DOM @$doc->loadHTML($content); //extract the text of a DIV element for PDF $text_for_pdf = $doc->getElementById('page_content')->nodeValue; //use FPDF $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',12); //specify width and height of the cell Multicell(width, height, string) //load extracted text into FPDF $pdf->Multicell(190,10, $text_for_pdf); $pdf->Output(); //output the file ?>
New question is currently disabled!