How to efficiently transfer HTML to PDF

Ideas and possibilities while developing websites are enormous. We often challenge with some new things where we also need to find an answer on Google.

If you’re searching how to convert HTML to PDF, stop looking on the other sites and continue to read this one!

Converting HTML to PDF

When converting HTML with CSS to pdf people often encounter problems, whether is the font rendering, css-float, positioning elements to problems with memory on the server side. The main question is often how do I really need to write CSS for PDF, does it has some hidden features that will make all things work.

Well, the answer is that there are no shortcuts. Some libraries will do the most job for you if you keep HTML simple, but when you need to change something to more complicated you will probably encounter different results with rendering, converting time, memory usage and so on.

People often use converters on the server side because it has more libraries, and you can more easily and directly store data on a server, or pass additional headers to show the content of PDF in browser. There aren’t many client-side converters.

Since converters are expensive if you are using simple HTML it would be better to use client-side script to free up server resources. In this example, we are only going to examine server side converters.
How to efficiently transfer HTML to PDF

Let’s get started

We will examine converting time, memory consumption, and visual look for a couple of HTML renderers.

We are going to use 3 templates in this example. One is fairly simple, and other two are more complex regarding code structure, css or the amount of data that needs to be converted. We are going to rate results from 1 to 5 scale, 1 is the poor result and 5 is the excellent results.

All of the tests were done on the same machine (running Linux os) a couple of times, and average results were taken for each group of data.

We will only valuate free open source PDF converters

You can preview HTML templates that we used in our testings

Simple HTML
Complex HTML
Long HTML

Installation

DOMPDF:
(Os: universal, runs in php) Very easy installation (Under one minute). It uses composer to download and install dependencies. It also has a zip file if you don’t want to use it with composer. It’s a mostly a CSS 2.1 compliant HTML layout and rendering engine written in PHP.

It’s a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
wkhtmltopdf:
(os: windows, mac os, linux, “SmartOS “, “OpenBSD”). Easy download and installation, under a minute. Uses Qt WebKit rendering engine.

“These run entirely “headless” and do not require a display or display service.”. In this demonstration, we are going to use 64 bit version
mPDF:
(os: universal, runs in php). Easy download and installation, under a minute.

It uses composer to download and install dependencies. mPDF is a PHP class which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF, with a number of enhancements.
TCPDF:
(os: universal, runs in php). Easy download and installation, under a minute.

No dependency required to generate simple HTML. NOTE: There is a new version of TCPDF under development, but i was having difficulties running this new development version.

Testings

Simple HTML:

  Time Memory Result
DOMPDF 143ms 4.718592 MB 1
wkhtmltopdf 541ms 31.04MB 4
mPDF 1013ms 16.252928 MB 5
tcpdf 1316ms 6.815744 MB 1
 
 
 




 
 

Complex HTML

  Time Memory Result
DOMPDF 1559ms 36.962304 MB 2
wkhtmltopdf 17427ms 108.252 MB 5
mPDF 18577ms 23.59296 MB 4
tcpdf 44825ms 97.517568 MB 2
 
 
 



 
 

Long HTML

  Time Memory Result
DOMPDF 30000ms 134.217728 MB OUT OF MEMORY 1
wkhtmltopdf 654ms 21.341683 MB 5
mPDF 7615ms 78.905344 MB 5
tcpdf 123864ms 6.915832 MB 4
 
 
 



 
 

We also tried to generate PDF-s using Html2Pdf converter, but results were very poor and it took time to even generate something out of our examples.

Getting into details

As seen in the results if you are going to use simple HTML, for example, simple invoices for generating PDF you will probably prefer mpdf. It provided excellent results, with 16 mb of usage, wkhtmltopdf did a decent job too, generating in halftime of mpdf but used twice as much memory as mpdf.

DOMPDF and tcpdf would require a HTML optimization to get to desire results.
For a complex html, we definitely recommend wkhtmltopdf. Although it uses a large amount of memory, the results were excellent.

Perhaps running it as separate service is good choice, but that depends on how much often you expect to generate complex PDFs. It also took some time to generate PDF, around 18 seconds, but if that’s the cost to have a excellent result i’ll take it. Mpdf was struggling to render correctly column sizes, and it lacks some css details, but hey if you don’t need them you can live with mpdf too because it used only 23mb of memory in compared to wkhtmltopdf which used 108. Other libraries are not so worth to be mentioned here.

In the long example, we used pretty simple html, enough to generate around 15 pages. People often need to generate large but simple PDFs. For example some database export. Once again wkhtmltopdf and mpdf delivered good results.

Wkhtmltopdf took the win in speed and memory taking only 654ms to generate pdf and 21mb of ram, while mpdf took about 7 seconds and a bit more memory around 80mb, both delivered excellent results. It seems like tcpdf would run forever but around 2 minutes of execution delivered good results using only 6mb of ram, but that time is simply too much for anyone to wait and seems to be funny when we look at a half second for wkhtmltopdf.

Dompdf produced out of memory exception, which is often the problem whit DOMPDF as I can see on StackOverflow.

It’s worth to mention that wkhtmltopdf has some other features like running Javascript inside, and you can get page number from wkhtmltopdf where you can inject page number to certain HTML element you choose. You can also provide headers and footers just like in tcpdf library.

Wkhtmltopdf gives even more variables to play with it.

Conclusion

Although results vary in many ways, it’s safe to say that mpdf and wkhtmltopdf will get you where you need quickly and with good results.

For DOMPDF it will take you some time to add for example fonts to PDF, and to satisfy html to have a good looking PDF.

We used dompdf in some simple renderings, but we quickly change it to wkhtmltopdf when it comes to more complex solutions.

Of course, there are maybe some better PDF converters online but I would say by googling these 4 – 5 pdf converters are mainly in conversations.

Explore next

Related posts