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();
?>
PHP
- 1
require('fpdf17/fpdf.php');
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
PHP
- 1
$pdf->SetFont('Arial','B',16);
PHP
- 1
$pdf->Cell(40,10,'Hello World !',1);
PHP
- 1
$pdf->Cell(60,10,'Powered by FPDF.',0,1,'C');
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
?>