Generate PDF from HTML template in PHP using Dompdf

By Faruque Ahamed Mollick

Here in this post, we are going to see how to generate the PDF from an HTML template.

In brief, Dompdf is the HTML to PDF converter that is written in PHP and can be used by developers to generate PDF. This is mostly a CSS 2.1 compliant HTML layout and rendering engine. A few CSS3 properties also can be handled by Dompdf.

Now let’s see the how to generate PDF from an HTML template using the Dompdf PDF converter.

To do this first you have to download Dompdf. After you download it place it in a directory.

Here we are going to create a simple HTML file in the same directory where we have placed the Dompdf. For example, our HTML file has a table that contains some dummy data inside it. Below is the code of that HTML file that we are going to convert into PDF:

<!DOCTYPE html>
<html>
<head>
  <title>PDF Create</title>
  <style type="text/css">
    th, td {
      border: solid 1px #777;
      padding: 2px;
      margin: 2px;
    }
  </style>
</head>
<body>

<img src="1.jpg">
<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Phone Number</th>
  </tr>
  <tr>
    <td>Faruque Ahamed Mollick</td>
    <td>[email protected]</td>
    <td>1234567890</td>
  </tr>
  <tr>
    <td>Ansaruddin SK</td>
    <td>[email protected]</td>
    <td>9876543210</td>
  </tr>
  <tr>
    <td>Mintu SK</td>
    <td>[email protected]</td>
    <td>5555555555</td>
  </tr>
  <tr>
    <td>Dewan Ukil</td>
    <td>[email protected]</td>
    <td>8888555596</td>
  </tr>
  <tr>
    <td>Aryan Khan</td>
    <td>[email protected]</td>
    <td>7777777777</td>
  </tr>
  <tr>
    <td>Arijit Singh</td>
    <td>[email protected]</td>
    <td>9999999999</td>
  </tr>
</table>

</body>
</html>

Name the HTML file as test.html.

Now I am going to tell you how to use the Dompdf to generate PDF from the test.html file.

At first, you need to include the required PHP libraries from Dompdf directory that you have just download:

require_once 'dompdf/lib/html5lib/Parser.php';
require_once 'dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';
require_once 'dompdf/lib/php-svg-lib/src/autoload.php';
require_once 'dompdf/src/Autoloader.php';

After that you just need few lines of codes:

Dompdf\Autoloader::register();

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml( file_get_contents('test.html') );

$dompdf->setPaper('A4', 'landscape');

$dompdf->render();

//$dompdf->stream();
// View PDF on browser instead of download
$dompdf->stream("dompdf_outFRK.pdf", array("Attachment" => false));

The complete PHP code is given below:

<?php
  require_once 'dompdf/lib/html5lib/Parser.php';
  require_once 'dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';
  require_once 'dompdf/lib/php-svg-lib/src/autoload.php';
  require_once 'dompdf/src/Autoloader.php';
  Dompdf\Autoloader::register();

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml( file_get_contents('test.html') );

$dompdf->setPaper('A4', 'landscape');

$dompdf->render();

//$dompdf->stream();
// View PDF on browser instead of download
$dompdf->stream("dompdf_outFRK.pdf", array("Attachment" => false));

?>

Name the PHP files as any name you like and run it on your browser. After you run the above code you will see the PDF on your browser. If you want it to be downloaded instead of showing on the browser just replace the below code:

$dompdf->stream("dompdf_outFRK.pdf", array("Attachment" => false));

into

$dompdf->stream();

And you will see the PDF downloading instantly instead of showing it on the browser.

You will see that the same HTML template is now in PDF format. All of these done by Dompdf. I hope you will also like to use Dompdf to generate PDF from HTML template.

Leave a Reply

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