Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials
And Offer Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development Right Via Email.

Convert HTML To PDF Using PHP

Friday, Aug 19, 2016

Tags:- PHP HTML


In this tutorial we will show you how to convert HTML to PDF using PHP with the help of DOMPDF library.Using this method you can convert HTML into PDF with inline styling support.You can also download the converted PDF using this method.


Convert HTML To PDF Using PHP


To Convert HTML To PDF It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to generate PDF
  3. Make a CSS file and define styling


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name html_to_pdf.html

<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="wrapper">

<div id="html_div">
 <form action="generate_pdf.php" method="post">
  <input type="text" name="name" placeholder="Enter Name">
  <br>
  <input type="text" name="email" placeholder="Enter Email">
  <br>
  <input type="text" name="age" placeholder="Enter Age">
  <br>
  <input type="text" name="country" placeholder="Enter Country">
  <br>
  <input type="submit" name="submit_val" value="GENERATE PDF">
 </form>
</div>

</div>
</body>
</html>

In this step we create a form to send details to 'generate_pdf.php' page and add 'style.css' which we were going to create in next step.



Step 2.Make a PHP file to generate PDF

We make a PHP file and save it with a name generate_pdf.php

<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';

if(isset($_POST['submit_val']))
{
$dompdf = new Dompdf(); 
$dompdf->loadHtml('
<table border=1 align=center width=400>
<tr><td>Name : </td><td>'.$_POST['name'].'</td></tr>
<tr><td>Email : </td><td>'.$_POST['email'].'</td></tr>
<tr><td>Age : </td><td>'.$_POST['age'].'</td></tr>
<tr><td>Country : </td><td>'.$_POST['country'].'</td></tr>
</table>
');
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("",array("Attachment" => false));
exit(0);
}
?>

You have to download dompdf 0.7.0 to convert HTML to PDF.In this step we add 'autoload.inc.php' file and then create Dompdf object.We get all the values entered by the user and them embed that values in table for displaying in PDF file.You have to insert all the your HTML in loadHtml() function and then we set page type to A4 and in landscape mode and then use render() function to convert HTML to PDF and then display the created pdf file using stream() function.If you have to download pdf instead of showing use stream() function without any parameters.You can find more details about methods and function in there website.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#585858
}
#wrapper h1 p
{
 font-size:18px;
}
#wrapper a
{
 color:blue;
 font-size:20px;
}
#html_div input[type="text"]
{
 margin-top:5px;
 width:250px;
 height:35px;
 padding:10px;
}
#html_div input[type="submit"]
{
 background-color:#585858;
 width:250px;
 height:35px;
 border:none;
 margin-top:5px;
 color:white;
 margin-left:-5px;
}

That's all, this is how to convert HTML to PDF using PHP and HTML.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email