Generate PDF from HTML with Dompdf in PHP

The Pdf file creation in PHP mainly requires when need to generate the file on the basis of the available data otherwise, 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. Basic example
  2. Preview of the file
  3. Save to server
  4. Conclusion

1. Basic example

Download the library from here.

Store HTML string in $html and assign file name in $filename.

Include autoload.inc.php.

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

Completed Code

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

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

2. 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 forces the browser to open the download dialog. So, instead of 1 pass 0.

Completed Code

<?php

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

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

3. Save to server

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

Completed Code

<?php 

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

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


4. 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.

If you found this tutorial helpful then don't forget to share.