Convert HTML file into PDF in PHP

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:

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.

 

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments