PHP 7 DomPDF Library Script to Generate PDF Document From HTML Template in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be generating pdf document from html template using dompdf library in browser using php 7 . All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

 

index.html

 

 

<!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>faruque@domain.com</td>
    <td>1234567890</td>
  </tr>
  <tr>
    <td>Ansaruddin SK</td>
    <td>ansaruddin@domain.com</td>
    <td>9876543210</td>
  </tr>
  <tr>
    <td>Mintu SK</td>
    <td>mintu@domain.com</td>
    <td>5555555555</td>
  </tr>
  <tr>
    <td>Dewan Ukil</td>
    <td>dewan@domain.com</td>
    <td>8888555596</td>
  </tr>
  <tr>
    <td>Aryan Khan</td>
    <td>aryan@domain.com</td>
    <td>7777777777</td>
  </tr>
  <tr>
    <td>Arijit Singh</td>
    <td>arijit@domain.com</td>
    <td>9999999999</td>
  </tr>
</table>

</body>
</html>

 

 

 

Now create a index.php file which will create the pdf document from the above template using dompdf library as shown below

See also  PHP 7 Script to Build a Login Form Using HTML5 and MySQL Database in Browser Full Project For Beginners

 

 

index.php

 

 

 

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

?>

Leave a Reply