Sunday, May 27, 2012

Convert HTML + CSS to PDF with PHP?


Ok, I'm now banging my head against a brick wall with this one.



I have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML.



I'm now after a way of converting it to PDF. I have tried:




  • DOMPDF : it had huge problems with tables. I factored out my large nested tables and it helped (before it was just consuming up to 128M of memory then dying--thats my limit on memory in php.ini) but it makes a complete mess of tables and doesn't seem to get images. The tables were just basic stuff with some border styles to add some lines at various points;


  • HTML2PDF and HTML2PS : I actually had better luck with this. It rendered some of the images (all the images are Google Chart URLs) and the table formatting was much better but it seemed to have some complexity problem I haven't figured out yet and kept dying with unknown node_type() errors. Not sure where to go from here; and


  • Htmldoc : this seems to work fine on basic HTML but has almost no support for CSS whatsoever so you have to do everything in HTML (I didn't realize it was still 2001 in Htmldoc-land...) so it's useless to me.



I tried a Windows app called Html2Pdf Pilot that actually did a pretty decent job but I need something that at a minimum runs on Linux and ideally runs on-demand via PHP on the Webserver.



I really can't believe I'm this stuck. Am I missing something?


Source: Tips4all

19 comments:

  1. Have a look at PrinceXML.

    It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming is not free either, so if it saves you 10 hours of work, you're home free.)

    Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2!?

    http://princexml.com/samples/

    ReplyDelete
  2. Have a look at WKHTMLTOPDF . It is open source, based on webkit and free.

    We wrote a small tutorial here.

    ReplyDelete
  3. After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

    HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

    A bug was raised against PHP 5.2.1 for this, which is still open almost two years later.

    What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

    So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:


    pcre.backtrack_limit = 2000000; # probably more than I need but that's OK
    memory_limit = 1024M; # yes, one gigabyte; and
    max_execution_time = 600; # yes, 10 minutes.


    Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

    Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

    Unfortunately, that's the best I've come up with.

    ReplyDelete
  4. Why dont you try MPDF version 2.0?..
    I used for creating PDF document.Its working fine...

    ReplyDelete
  5. I suggest http://docraptor.com (which uses PrinceXML as the "engine")

    ReplyDelete
  6. Just to bump the thread, I've tried DOMPDF and it worked perfectly. I've used divs and other block level elements to position everythign. Kept it strictly CSS2.1 and it played nicely.

    ReplyDelete
  7. Checkout TCPDF. It has some HTML to PDF functionality that might be enough for what you need. It's also free!

    ReplyDelete
  8. Thanks to the person who posted the "WKHTMLTOPDF" suggestion.

    I was previously using "mPDF" which does a decent job of rendering HTML and CSS, however due to a recent issue with "nested tables", I am going to try "WKHTMLTOPDF".

    So far, I've tested it with a few websites, and found it to "lightning fast", and "pretty damn accurate" in terms of rendering as intended.

    Given it's based on WebKits, I'm sure it'll do most websites without issue.

    * One thing to note: in using it in a "web-app" you'll need to build a wrapper. For any programmer, this should be a breeze. *

    -Peter

    ReplyDelete
  9. Fine rendering doesn't mean anything. Does it validate?

    All browsers do the most they can to just show something on the screen, no matter how bad the input. And of course they do not do the same thing. If you want the same rendering as FireFox, you could use its rendering engine. There are pdf generators for it. It is an awful lot of work, though.

    ReplyDelete
  10. Perhaps you might try and use Tidy before handing the file to the converter. If one of the renderer chokes on some HTML problem (like unclosed tag), it might help it.

    ReplyDelete
  11. There's a tutorial on Zend's devzone on generating pdf from php (part 1, part 2) without any external libraries. I never implemented this sort of solution, but since it's all php, you might find it more flexible to implement and debug.

    ReplyDelete
  12. I am using fpdf to produce pdf files using php. It's working well for me so far to produce simple outputs.

    ReplyDelete
  13. I dont think a php class will be the best for render an xHtml page with css.

    What happen when a new css rule come out? (soon css 3.0...)

    The best way to render an html page is, obvisiuly, a browser.
    Firefox 3.0 can natively 'print' in pdf format, torisugary developed an extension (command line print) to use it. Here you'll find it.

    Anyway, there are still many problmes runninr firefox just as a pdf converter...

    At the moment, i think that wkhtmltopdf is the best (that is the one used by the safari browser), fast, quick, awesome.
    Yes, opensource as well...
    Give it a look

    ReplyDelete
  14. Well if you want to find a perfect XHTML+CSS to PDF converter library, forget it, it's far from possible. Because it's just like finding a perfect browser (XHTML+CSS rendering engine). Do we have one? IE or FF?

    I have had some success with DOMPDF. The thing is that you have to modify your HTML+CSS code to go with the way the library is meant to work. Other than that, I have pretty good results. See below.

    Original HTML: http://www.nutquote.com/quote/William_Shakespeare/66/simple

    PDF: http://www.converthub.com/htmltopdf.php?html=http://www.nutquote.com/quote/William_Shakespeare/66/simple

    ReplyDelete
  15. Try grabbing the latest nightly dompdf build - I was using an older version that was a terrible resource hog and took forever to render my pdf. After grabbing a nightly from here:

    http://eclecticgeek.com/dompdf/

    It only took a few seconds to generate the PDF - AND it was just as nicely rendered as with PrinceXML / Docraptor. Seems like they've seriously optimized the dompdf code since I last used it!

    ReplyDelete
  16. This question is pretty old already, but haven't seen anyone mentioning CutyCapt so I will :)

    CutyCapt


    CutyCapt is a small cross-platform command-line utility to
    capture WebKit's rendering of a web page into a variety of vector and
    bitmap formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, and BMP

    ReplyDelete
  17. Darryl Hein's mention above of TCPDF (http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf) is likely a great idea. Nicola Asuni's code is pretty handy and powerful. The only killer is if you ever plan on merging PDF files with your generated PDF it doesn't have those features. You would have to create the PDF and then merge it using something like PDFTK by Sid Steward (www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).

    ReplyDelete
  18. I would suggest dompdf. This is what is new when i searched google. Since this question was asked 2 years ago i think dompdf wouldn't had come into existence. Besides i created a little note in my site about the usage of dompdf with sourcode and examples... people may find it useful else it is left to the moderator. here it is...

    http://vikku.info/programming/php/html-to-pdf-conversion/html-to-pdf-conversion-in-php-convert-html-to-pdf-using-dompdf-in-php.htm

    ReplyDelete
  19. Good news! Snappy!!

    Snappy is a very easy open source PHP5 library, allowing thumbnail, snapshot or PDF generation from a url or a html page. And... it uses the excellent webkit-based wkhtmltopdf

    Enjoy! ^_^

    ReplyDelete