Categories: PHP

Convert HTML to PDF Using DOMPDF in PHP

How to convert HTML to PDF ?

DOMPDF is the open source PHP library which is used to convert html to PDF files, it has many features along with few limitations.

Almost many web applications needs to have this feature, we developers always been requested to add generate report feature, like wise I found DOMPDF good in certain ways basically it’s fulfilling my applications requirements and working well so far.

I decided to write a simple tutorial to guide on generating PDF report from HTML content.

Before starting to use DOMPDF please have a look at limitations along with the features it’s provides, you can just check with your needs and if sounds good then go ahead and use the plugin.

DOMPDF Limitations:

  • Large file or HTML content with large table can take longer than usual execution time. (If you have big content coming on your way, I would prefer you should find another alternative or may be other paid plugins)

DOMPDF Features:

  • Can work with CSS 2.1 along with few basic CSS3 properties
  • HTML 4.0
  • External style sheet – we can embed external CSS file within HTML content
  • Complex Tables – It works with Nested tables as long as we have limited data.
  • Inline PHP support
  • Basic SVG support

Let’s get started:

DOMPDF requires following configuration on your server, make sure you have those extension and libraries are ready to use with PHP

  • PHP > 5.3
  • DOM extension
  • GD extension
  • MB String extension
  • php-font-lib
  • php-svg-lib

Step 1: Installation:

Install DOMPDF from Github:

git clone https://github.com/dompdf/dompdf.git

Install php-font-lib: (to dompdf directory)

git clone https://github.com/PhenX/php-font-lib.git lib/php-font-lib

Install php-svg-lib (to dompdf directory)

git clone https://github.com/PhenX/php-svg-lib.git php-svg-lib

So now we are ready to use the plugin.

Step 2: Generate Sample PDF

<?php
require_once __DIR__ . '/dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();

$html = <<<'ENDHTML'
<html>
 <body>
  <h1>Generated By DOMPDF</h1>
  <p style="color: green;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
  <p style="color: red;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
  <p style="color: blue;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
 </body>
</html>
ENDHTML;

$dompdf->loadHtml($html);

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

$dompdf->render();

$dompdf->stream('mypdf.pdf');
?>

A Quick description on execution:

In the above code first we have to include the  `autoload.inc.php` file, which is having dompdf settings, after including autoload.inc.php file we can simple reference DOMPDF namespace and then can initiate the object to use.

Loading HTML: `LoadHtml()` is used to load html content, we can also read content from external html file using `load_html_file()` function.

Saving Generated PDF file into the Specific Directory

If you needs to store pdf file on server into a specific directory you can easily use following code:

<?php
require_once __DIR__ . '/dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();

$html = <<<'ENDHTML'
<html>
 <body>
  <h1>Generated By DOMPDF</h1>
  <p style="color: green;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
  <p style="color: red;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
  <p style="color: blue;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p>
 </body>
</html>
ENDHTML;

$dompdf->loadHtml($html);

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

$dompdf->render();

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

If you get any issues using tutorial, you can easily comment in below section.

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

View Comments

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

1 year ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

1 year ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

1 year ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

1 year ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

1 year ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

1 year ago