Convert HTML to PDF Using DOMPDF in PHP

Convert HTML to PDF Using DOMPDF
Written by Yogesh Koli

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:

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

Install php-svg-lib (to dompdf directory)

So now we are ready to use the plugin.

Step 2: Generate Sample PDF

A Quick description on execution:

In the above code first we have to include the file, which is having dompdf settings, after including 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:


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

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

1 Comment threads
1 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Yogesh KoliThomas Lindner Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
Thomas Lindner
Thomas Lindner

And how do you load external css file?

Yogesh Koli

Just like we do on web pages.