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

19,641

Solution 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.

Solution 2

Try this:

Solution 3

Have you tried Prince?

Solution 4

Try HTMLDOC commandline tool project https://www.msweet.org/htmldoc/index.html

Solution 5

There are many solution to convert HTML to PDF, I can suggest you the one by https://grabz.it.

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
include("GrabzItClient.class.php");

// 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
$grabzIt->URLToPDF("http://www.google.com");

// To save in case public callback handler is available
$grabzIt->Save("http://www.example.com/handler.php");   
// 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";
$grabzIt->SaveTo($filepath);    

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

Share:
19,641

Related videos on Youtube

Santosh S
Author by

Santosh S

Updated on September 01, 2020

Comments

  • Santosh S
    Santosh S almost 4 years

    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 http://html2fpdf.sourceforge.net
    4. html2pdf http://html2pdf.fr/
    5. dompdf http://code.google.com/p/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 (http://www.tufat.com/s_html2ps_html2pdf.htm)

      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 (http://www.ultrashareware.com/html-to-pdf.htm)

    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?

    • Tuy Sotheara
      Tuy Sotheara almost 15 years
      The error "Gtk-WARNING **: cannot open display: :0.0" is because the app uses the windowing system. I would guess that the error occurs because the app tries to open the PDF after its generation?
    • Santosh S
      Santosh S almost 15 years
      no , it not open pdf after generation. But it open a small window while using it in console.
    • rjmunro
      rjmunro almost 14 years
      Because there are so many questions similar to this one but not quite the same, I decided to try to collect a complete list of HTML to PDF converters into a community wiki question stackoverflow.com/questions/3178448/…
    • Martin Thoma
      Martin Thoma over 6 years
  • Santosh S
    Santosh S almost 15 years
    can you suggests any on-line service and their URL/link
  • Admin
    Admin almost 15 years
    And this could take a while to create big set of PDF.
  • Santosh S
    Santosh S over 14 years
    but, what about links or anchor tag in html pages?
  • Santosh S
    Santosh S over 14 years
    do you know any command line app for same apart from those mentioned in question?
  • Santosh S
    Santosh S over 14 years
    @FDisk , I already tried that and I also mentioned same in my question
  • Santosh S
    Santosh S over 14 years
    hmmmmmm? but its expensive, cost USD 495 !
  • Randell
    Randell over 14 years
    For PHP, i've only used dompdf, and I only have to print an average of 3 pages per call. The only other PDF generator i've used is JasperReports, but I think it's only for Java. Maybe you could most the entire stack trace of the error you're getting from the command line.
  • FDisk
    FDisk over 14 years
    freepdfconvert.com has no support for flash files and javascript. And generated pdf is not exactly as the website looks. for example i tried pazintys.com
  • rjmunro
    rjmunro almost 14 years
    That is a terrible solution. It will turn all the text into bitmap graphics. It will use screen css instead of print css. It will only show as much of the page as can fit in a screen capture. There are plenty of ways to do it better. Please don't do this!
  • Julie
    Julie over 13 years
    DocRaptor.com uses Prince as the engine (so you get the same awesome results), but it's much cheaper (free for small plans).
  • Admin
    Admin over 13 years
    Hmm. Your answer history would suggest you have some sort of interest in this service Julie - how about a little full disclosure eh?
  • andho
    andho almost 13 years
    It fails poorly with Multipage tables
  • andho
    andho almost 13 years
    Prince rocks, but too expensive :(
  • andho
    andho almost 13 years
    Poor formatting, does not support CSS2.1 yet
  • Carlos2W
    Carlos2W over 8 years
    No it doesn't. You just have to handle this kind of problem with css : stackoverflow.com/questions/1763639/…