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@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';

// 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 force the browser to open 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@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';

// 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@gmail.com</td>
        </tr>
        <tr>
            <td>vishal</td>
            <td>vishal@gmail.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.

Related Post

Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  

4 Comments

  1. Steve said:

    Thank you.
    This is what I’ve been looking for
    Steve

    February 12, 2018
    Reply
  2. Shahrin Ana said:

    I want to know that how can i make a pdf of php table. The example you have given for a html static table. I am looking forward dynamic table. i can’t get what to do.

    October 23, 2018
    Reply
    • Yogesh Singh said:

      Hi Shahrin,
      Just read records from your MySQL database and create the table layout accordingly and append in $html.

      If you still facing the issue then you can mail me code at makitweb@gmail.com so I can check it.

      October 23, 2018
      Reply
  3. debabrata roy said:

    now this things create a pdf with default padding.
    1. How to remove that?
    2. how to set page break in this method of creating pdf using dompdf?

    November 26, 2018
    Reply

Leave a Reply

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