Convert HTML + CSS to PDF with PHP

Question: 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:

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—that is 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 Web server. I really can't believe I'm this stuck. Am I missing something?

Answer: After some investigation and general hair-pulling, the solution seems to be VeryPDF HTML to Any Converter Command Line for Linux. There are three versions of this software: Mac, Linux and Windows. So if you need to run it on Linux, this software can satisfy your needs. And this software does really good job with tables, borders and even moderately complex layout. And even if you run the conversion from HTML to PDF in a large amount by this software, it should not have any problem.

And as the different browsers will have different showing effect,  but 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 Fire Fox, you could use its rendering engine.  Here are some command line examples for your reference:

Use HTML document as cover. It will be inserted before the toc with no headers
and no footers:
html2any -cover http://www.google.com/ http://www.verypdf.com/ _cover.pdf

Show JavaScript debugging output:
html2any -debug-javascript http://www.verypdf.com/ _debug-javascript.pdf

Remove external links:
html2any -disable-external-links http://www.verypdf.com/ _disable-external-links.pdf

Remove internal links:
html2any -disable-internal-links http://www.verypdf.com/ _disable-internal-links.pdf

If you need to know more detail CSS code or PHP code, please contact us as soon as possible.

VN:F [1.9.20_1166]
Rating: 5.3/10 (3 votes cast)
VN:F [1.9.20_1166]
Rating: -1 (from 1 vote)
Convert HTML + CSS to PDF with PHP, 5.3 out of 10 based on 3 ratings

Related Posts

This entry was posted in HTML Converter (htmltools) and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!