Development Tutorials 4 min read

# How to efficiently transfer HTML to PDF

Find out what's the best way for transfering HTML to PDF in our short and actionable article!

Last updated on September 15, 2025

## Key takeaways

1. Converting HTML to PDF has no shortcuts, and simple HTML converts well with most libraries while complex layouts produce varying rendering, speed and memory results.
2. For simple HTML such as invoices, mPDF gave excellent results using about 16 MB of memory, while wkhtmltopdf was faster but used about twice the memory.
3. For complex HTML, wkhtmltopdf produced the best output but took around 18 seconds and 108 MB of memory, so running it as a separate service can make sense.
4. For a long but simple 15-page document, wkhtmltopdf took only 654 ms and 21 MB, while DOMPDF ran out of memory and TCPDF needed about two minutes.
5. wkhtmltopdf can also run JavaScript, inject page numbers into chosen HTML elements and add headers and footers to generated PDFs.

![transferring HTML to PDF](https://factory.dev/wp-content/uploads/2026/11/html-to-pdf.webp)

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.

![transferring HTML to PDF](https://factory.dev/wp-content/uploads/2026/11/html-to-pdf.webp)

## 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](https://api.factory.hr/wp-content/uploads/2016/10/simple.html)
 [Complex HTML](https://api.factory.hr/wp-content/uploads/2016/10/complex.html)
 [Long HTML](https://api.factory.hr/wp-content/uploads/2016/10/long.html)

## Installation

[DOMPDF](https://github.com/dompdf/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](https://github.com/wkhtmltopdf/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](https://github.com/mpdf/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](https://github.com/tecnickcom/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](https://api.factory.hr/wp-content/uploads/2016/10/simple.pdf) |
| wkhtmltopdf | 541ms | 31.04MB | [4](https://api.factory.hr/wp-content/uploads/2016/10/simple-2.pdf) |
| mPDF | 1013ms | 16.252928 MB | [5](https://api.factory.hr/wp-content/uploads/2016/10/simple-1.pdf) |
| tcpdf | 1316ms | 6.815744 MB | [1](https://api.factory.hr/wp-content/uploads/2016/10/simple-3.pdf) |

### Complex HTML

|  | Time | Memory | Result |
| --- | --- | --- | --- |
| DOMPDF | 1559ms | 36.962304 MB | [2](https://api.factory.hr/wp-content/uploads/2016/10/complex.pdf) |
| wkhtmltopdf | 17427ms | 108.252 MB | [5](https://api.factory.hr/wp-content/uploads/2016/10/complex-2.pdf) |
| mPDF | 18577ms | 23.59296 MB | [4](https://api.factory.hr/wp-content/uploads/2016/10/complex-1.pdf) |
| tcpdf | 44825ms | 97.517568 MB | [2](https://api.factory.hr/wp-content/uploads/2016/10/complex-3.pdf) |

### Long HTML

|  | Time | Memory | Result |
| --- | --- | --- | --- |
| DOMPDF | 30000ms | 134.217728 MB OUT OF MEMORY | 1 |
| wkhtmltopdf | 654ms | 21.341683 MB | [5](https://api.factory.hr/wp-content/uploads/2016/10/long-1.pdf) |
| mPDF | 7615ms | 78.905344 MB | [5](https://api.factory.hr/wp-content/uploads/2016/10/long.pdf) |
| tcpdf | 123864ms | 6.915832 MB | [4](https://api.factory.hr/wp-content/uploads/2016/10/long-2.pdf) |

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.

## Related

[Business &amp; Consulting **How Much of Your Catalog Production Should You Automate?** Which catalog tasks can follow rules, and which need a designer? Compare three publishing approaches and find the right balance for your team. 4 min read](https://factory.dev/blog/how-much-of-your-catalog-production-should-you-automate) [Company News **Webinar: When Product Data and Design Don’t Align – Fixing the Root Cause of Catalog Delays** Join Factory and Pimcore on September 15 to learn how connected product data and design workflows can reduce manual work, errors, and catalog delays. 2 min read](https://factory.dev/blog/webinar-fixing-catalog-production-delays) [Martech &amp; Digital Experience **Product Catalog Preparation: Common Challenges and How to Fix Them** Explore the most common catalog preparation challenges and how a connected data-to-design process helps teams work faster, reduce errors, and deliver accurate catalogues on time. 9 min read](https://factory.dev/blog/product-catalog-challenges-how-to-fix)

---

Source: https://factory.dev/blog/transfer-html-to-pdf
