Converting HTML to PDF (not PDF to HTML) using PHP

I am a PHP developer and in one of my projects, I need to convert some HTML documents (about 30 to 50 pages) into PDF documents.

My search has turned up the following possible solutions. Among them are some PHP libraries and some command line applications. Each has its own advantages and disadvantages.

PHP libraries:

  1. fpdf (need more effort to convert)
  2. tcpdf (need more effort to convert)
  3. html2fpdf
  4. html2pdf
  5. dompdf (compared to other, works well)

For each library, I have problems like:

  1. Takes a long time (more than five minutes to convert 30 HTML pages)
  2. Requires too many resources (memory and time)

    (I set the following parameters in php.ini:

    max_execution_time = 600
    memory_limit = 250M

    but things still don’t work.)

  3. Needs HTML pages to be well-formatted (e.g. no missing close tags)

All of these work when I try to convert simple HTML docs (five or fewer pages with little CSS)

Command line applications

All command line apps work perfectly and very quickly compared to the above libraries, but only when I run them directly on console. When I try to use them in PHP with exec() or system(), they give me errors.

The following are the command line applications and their errors when I run them in PHP:

  1. html2pdf (

    html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0
    No protocol specified

  2. wkhtmltopdf

    Loading page: 10%
    Loading page: 33%
    Loading page: 100%
    Waiting for redirect
    Outputting pages
    QPainter::begin(): Returned false
    QPainter::begin(): Returned false
    QPainter::save: Painter not active
    QPainter::scale: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::setBrush: Painter not active
    QPainter::pen: Painter not active
    QPainter::setPen: Painter not active

  3. htmltopdf (

So now I am looking for help. Can anyone answer:

Which PHP library would work well in my case?

Why do these errors occur in command line applications?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Regarding wkhtmltopdf:

  • This thing works blazingly fast and it can also handle all kinds of HTML/CSS you throw at it, so when you need speed, you should seriosly consider it. We switch to it recently in our company and our PDF serving got enourmous speed-boost.
  • At least under Linux it needs XOrg libraries to be installed – servers usually don’t have them, so that might be your problem.

Method 2

Try this:

Method 3

Have you tried Prince?

Method 4

There are many solution to convert HTML to PDF, I can suggest you the one by

The have a flexible PHP API which can be used by cronjobs or directly from PHP web page.

If you want to try it, at first you should get an app key + secret for authorization and the development free SDK

Here is an example of a basic implementation.

//First init

// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" values for your account!
$grabzIt = new GrabzItClient("Application Key", "Application Secret");

// To take a PDF screenshot

// To save in case public callback handler is available
// OR To save in case public callback handler is not available,
// it's a synchonous method can be usedthe will force your application to wait 
// while the screenshot is created
$filepath = "images/result.jpg";

It’s possible to get other kinds of screenshots such as image screenshot and etc.

Method 5

Try HTMLDOC commandline tool project

Method 6

But what if You will use any online service and send Your HTML content over HTTP?
Of course most of them are not free.

Method 7

One possibility: having the script automatically:

  1. Take the web page
  2. Open that page in a web browser
  3. Take a screencap of that page
  4. Turn it into a PDF

step 4 is easy – there are plenty of PHP/cmdline libraries that will let you put images onto a pdf or convert them (eg, fpdf.)

For steps 1-3… you might could try looking at the code from here: Not sure if it would be relevant – it seems like it requires a lot of setup. Maybe their architecture could work?

Method 8

A couple of questions and suggestions:

  • Do you really need it converted to PDF? Why? In some cases, it would be better to stick with HTML.
  • Is upgrading the hardware of the server that generates the PDF an option? I asked this because if all the libraries that you’ve tried is taking too long to create, then your only option might be upgrading the server.
  • You might want to solve the problem with the command line error. If it gives the fastest results, then find a work around it.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x