convert html to pdf in php using fpdf

In many cases of web development we require to generate a pdf from html or convert html to pdf in php using fpdf or generate pdf dynamically after filling some form in php script. And some time it is very problematic for some developers. So in this tutorial i demonstrate how to convert html to pdf dynamically with fpdf library. I think after going through this article any developer easily solve this type of related problem and configure it in his/her web application quickly.
Here for that i am using a bootstrap form and fpdf library which is open source and can be download from here. In this library function all the require function already defined. So we have to set the parameter for passing to the defined function and get the output.
So learn here a simple tutorial How to Convert HTML to PDF with fpdf.
You have to download fpdf library and include it in your PHP file below settings and how to show tags, fonts and images in your pdf file.

Take a quick look at this live demo to understand the flow. Also you can download this script from here and can try in your localhost or live server where you want to do.

covert html to pdf in php using fpdf.

Let’s go with the live example step by step. First create the basic form for submit data. Here using Bootstrap for styling the form.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>How to create Contact Form using Bootstrap  | click4knowledge.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<style>
    .header {
        color: #36A0FF;
        font-size: 27px;
        padding: 10px;
    }
</style>
</head>
<body>
<div class="container">
<div class="page-header">
    <h1>Convert HTML to PDF in PHP with fpdf</h1>
</div>
<!-- Contact Form - START -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="well well-sm">
//action page where pdf will be create and shown into the browser
                <form class="form-horizontal" method="post" action="createpdf.php">
                    <fieldset>
                        <legend class="text-center header" style="width:70%;">Contact us<span><img src="images/logo.png"></span></legend>
                        <div class="form-group">
                            <div class="col-md-8">
                               <input id="name" name="name" type="text" placeholder="Name" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-8">
                                <input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            
                            <div class="col-md-8">
                            
                                <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12 text-center">
                                <button type="submit" class="btn btn-primary btn-lg">Submit</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- Contact Form - END -->
</div>
</body>
</html>

After that we have to create createpdf.php for pdf creation and show into the browser.

<?php
require('WriteHTML.php');
$pdf=new PDF_HTML();
$pdf->AliasNbPages();

////add page page automatically for its true parameter

$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
//add images or logo which you want
$pdf->Image('images/logo.png',18,13,33);
//set font style
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>Click4Knowledge Web Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
Website: <u>www.click4knowledge.com</u></para><br><br>How to Convert HTML to PDF with fpdf example');

//set the form of pdf

$pdf->SetFont('Arial','B',7);

//assign the form post value in a variable and pass it. 

$htmlTable='<TABLE>
<TR>
<TD>Name:</TD>
<TD>'.$_POST['name'].'</TD>
</TR>
<TR>
<TD>Email:</TD>
<TD>'.$_POST['email'].'</TD>
</TR>
<TR>
<TD>Phone:</TD>
<TD>'.$_POST['phone'].'</TD>
</TR>
</TABLE>';
//Write HTML to pdf file and output that file on the web browser.
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
$pdf->Output(); 
?>

In this way we can create pdf of form submission data. Mostly we are require for invoice creation in any web application to generate pdf after form submit. Thanks for watching this tutorial. You can try yourself and share with your friends. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming.
Support
If you need any help regarding its configuration please feel free to comment we love to help you.

21 responses to “convert html to pdf in php using fpdf”

  1. crorkservice says:

    My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  2. Gines says:

    Appreciate you sharing, great blog. Much thanks again. Great.

  3. Anonymous says:

    This post have resolved our problem,thank you very much and hope you writting more good articles.

  4. Guest says:

    I think this article is very helpful for people,it has solved our problem,thanks!

  5. Guest says:

    Usually posts some incredibly interesting stuff like this. If you are new to this site.

  6. Guest says:

    Here is a good Blog You might Discover Interesting that we encourage you to visit.

  7. Guest says:

    I am sure this paragraph has touched all the internet viewers, its really
    really good paragraph.

  8. hattie stocks says:

    Thanks for finally writing about convert html to pdf in php using fpdf Liked it!

  9. jessiepruitt says:

    What’s up, I desire to subscribe for this blog to get hottest updates, thus where can i do it please help out.

  10. lucianabatten says:

    Hey exceptional website! Does running a blog similar to this require a massive amount work? I’ve very little knowledge of coding but I
    was hoping to start my own blog in the near future.
    Anyways, if you have any recommendations or techniques for new blog owners please share.I know this is off topic however I simply had to ask.
    Kudos!

  11. DomingaKPu says:

    Hi there to all, how is the whole thing, I think every one is getting more from this site, and your views are fastidious for new people.

  12. Anonymous says:

    Very nice article. I certainly appreciate this site. Stick with it!

  13. Reamer says:

    Really appreciate you sharing this article. Want more.

  14. zelda says:

    Im grateful for the article. Much obliged.

  15. ELWIN says:

    Hello, what if i want to set width of the column, table what should I do ?

  16. abhineet says:

    Can we add complete HTML file and make it as PDF??

  17. nsharma says:

    Hello,
    Is it support bootstrap classes. For example i have created a page using bootstrap (put some panel, table etc with same color based on condition) now how to create create PDF of this html .

  18. Kaly says:

    Awesome. You are so cool.

Leave a Reply

Your email address will not be published. Required fields are marked *