How To Convert HTML TO PDF in PHP Tutorial

Hi friends, today we are going to solve a very common problem faced among php developers. Here is the free tutorial for ‘How to convert HTML to PDF in PHP’ with free source code and demo. To solve this, we will use FPDF.

Many times we require a product brochure or catalogues. It is extremely tedious to create brochures and catalogue for each product manually. So FPDF gives provision to create it dynamically. Here user can view and download that created file.

To Use FPDF Library, you need to download it from here.

After Downloading it, Just copy the folder into your directory and customize it according to your need. Customization process is 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.


Step 1:

HTML input file like this

<div class="floatL cont_lft_side">
Please fill in your details below.
<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 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 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 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>
<input class="or_btn_icon" type="submit" value="Submit">

Step 2:

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.


<?php require('fpdf.php'); class PDF extends FPDF { // Page header function Header() { // Arial bold 15 $this->SetFont('Arial','B',15);
    // Move to the right
    // Title
    $this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
    // Line break

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

// Instanciation of inherited class
$pdf = new PDF();

$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);



Thats all about creating pdf from html file using php.

Don’t forget to share your doubts in the comment box and also share this post on social media and with your friends becaus“You share, I share, let’s make the world aware”.

You may want to take a look at the following related posts:

Also for more awesome tutorials, please don’t forget to like our facebook page TryCatch Classes.

Bonus: We also give training on following topics:

                       1. PHP Training in Mumbai.

2. Bootstrap Training Course in Mumbai.

3.  Web Designing Training in Mumbai.

4. UI / UX Training.

5. IOS Training Institute in Mumbai.