Generate PDF from HTML with Dompdf in PHP

The Pdf file creation in PHP mainly requires when we need to generate the file on the basis of the available data otherwise, we simply create it manually with the external applications.

For example – generating the report, the user certificate, etc.

For generating pdf file I am using Dompdf library which generates the downloadable pdf file from HTML.

Generate Pdf from HTML with Dompdf in PHP


Contents

  1. Download and include
  2. Basic example
  3. Preview of the file
  4. Save to server
  5. Conclusion

 

1. Download and include

You can download the library from here and create a new file create.php, include autoload.inc.php in it.

Here, I also defined a string variable($html) which contains HTML layout and a filename.

<?php

$html = "<table border='1' width='100%' style='border-collapse: collapse;'>
 <tr>
 <th>Username</th><th>Email</th>
 </tr>
 <tr>
 <td>yssyogesh</td>
 <td>yssyogesh@makitweb.com</td>
 </tr>
 <tr>
 <td>sonarika</td>
 <td>sonarika@gmail.com</td>
 </tr>
 <tr>
 <td>vishal</td>
 <td>vishal@gmail.com</td>
 </tr>
 </table>";
 $filename = "newpdffile";

 // include autoloader
 require_once 'dompdf/autoload.inc.php';

 

2. Basic example

Instantiating Dompdf Class object and passing HTML string variable($html) in loadHtml() method. The render() method renders the HTML to pdf.

Completed Code

<?php

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream($filename);

 

3. Preview of the file

For displaying the preview of the file before it is being downloaded. You need to pass the second parameter in the stream() method which contains Attachment option name in the Array.

By default, Attachment option value is 1 which force the browser to open download dialog. So, instead of 1 passing 0.

Completed Code

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream($filename,array("Attachment"=>0));

 

4. Save to server

Saving the generated file on the server for this executing output() method after the render() and storing return value in a variable($output) and writing it in a file using file_put_contents() method.

Completed Code

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

$output = $dompdf->output();
file_put_contents("file.pdf", $output);


 

5. Conclusion

You can easily use this library in your project when you have to generate the pdf file. You need to create your HTML layout and store in a variable or you can directly pass it in loadHtml().

It has some limitations that you need to know –

  • not particularly tolerant to poorly-formed HTML input (using Tidy first may help).
  • large files or large tables can take a while to render
  • CSS float is not supported (but is in the works).

You can learn more about this from their documentation with the examples.

Spread the love

Related Post

Be First to Comment

Leave a Reply

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