Convert HTML file into PDF in PHP

Abhigyan Singh 12th Jul 2020

This tutorial will guide you to convert HTML file into PDF directly. Web programmers need to convert their html file into pdf dynamically and it is a huge problem for them to convert dynamically.

This article is written in php. Here I use FPDF Library to convert html into pdf. When user gives any input to this library it automatically creates a pdf file with that content.

This is very needful when we need to create product brochure or catalogue. It is very tedious task to create brochure or catalogue for each product manually. So FPDF gives provision to create it dynamically. Here user can view and download that created file.

Explanation of Converting HTML file into PDF in PHP

To Use FPDF Library, you need to download it from their official website FPDF.

After Download it, Just copy the folder into your directory and customize it according to your need. Customization process given below:

Here I am using a simple HTML form to get some user input data for PDF. When user input the data and submit it generate the pdf with this input data.

HTML input file like this

<div style="width:448px; margin:auto;"></div>
<form method="POST" action="generatePdf.php" id="frmCtnt" name="frmCtnt">

<div class="txt_stl">
<input type="text" value="Enter Your Full Name" name="name" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
</div>

<div class="txt_stl">
<input type="text" value="Enter Your Email Id" name="email" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
</div>

<div class="txt_stl">
<input type="text" value="Enter Your Contact Number" name="mobile" onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';">
</div>

<div class="txt_stl">
<textarea onblur="if(this.value=='')this.value=this.defaultValue;" onfocus="if(this.value==this.defaultValue)this.value='';" rows="" cols="" name="comment">Please Write your Query or Comment here.</textarea>
</div>

<input class="or_btn_icon" type="submit" value="Submit">

</form>
</div>

Now when user input the value in this form and submit it goes to generatepdf.php file where all the code for pdf creation is written.

generatepdf.php

<?php
require('fpdf.php');

class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image('logo.jpg',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
// Line break
$this->Ln(20);
}

// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);

$pdf->Cell(0,10,'Name : '.$_POST["name"],0,1);
$pdf->Cell(0,10,'Email : '.$_POST["email"],0,1);
$pdf->Cell(0,10,'Mobile : '.$_POST["mobile"],0,1);
$pdf->Cell(0,10,'Comment : '.$_POST["comment"],0,1);

$pdf->Output();
?>

On the top of the file we need to include fpdf.php file. It is the main library file.

On the above code Header function is used to create header of pdf file.

function Header()
{
// Logo
$this->Image('logo.jpg',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
// Line break
$this->Ln(20);
}

Footer function is used to create footer of the pdf file.

function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}

And the main body of the content can be call like this:

$pdf->Cell(0,10,'Name : '.$_POST["name"],0,1);

Thats all about creating pdf from html file. If you have query or suggestion please feel free to write in comment section below.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk