Open Source HTML to PDF Renderer with Full CSS Support

Open Source HTML to PDF Renderer with Full CSS Support

I've always used it on the command line and not as a library, but HTMLDOC gives me excellent results, and it handles at least some CSS (I couldn't easily see how much).

Here's a sample command line

htmldoc --webpage -t pdf --size letter --fontsize 10pt index.html > index.pdf

How Can I add HTML And CSS Into PDF

Important:
Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2019. Online alternatives are better today at this than they were back then.

Here are some online services that you can use:

  • PDFShift
  • Restpack
  • PDF Layer
  • DocRaptor
  • HTMLPDFAPI
  • HTML to PDF Rocket

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 might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)

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

PrinceXML Samples

How would I convert a HTML5 / CSS3 document to PDF?

The only think I can think of that might work is wkHTMLtoPDF. It's a QT app that sits on top of WebKit to generate its PDF.

The good news is that it even evaluates JS so just about anything goes.

The other good news is that QT is available across a wide selection of platforms. Whatever you might be using, chances are you can use QT.

Render PDF in iTextSharp from HTML with CSS

It's not possible right now but nothing stops you from starting open-source project that will do it. I might actually start one, because I need it too!

Basically you will need parser that will convert html and css markup into iTextSharp classes. So <table> becames iTextSharp.SimpleTable and so on.

It would be easy to come up with prototype that would be able to work with limited html and css subset.

Update: Until the time this will be possible, this is how I temporarily resolved it for myself. Only two steps:

  • Tell your users to download open-source app called PDFCreator
  • Make all your html reports printer friendly by providing stylesheets for print.

    If some of your multi-page reports need to have headers on every page, set them up in THEAD html tag.

Now users will be able to print-friendly and if they choose PDFCreator printer driver, they will even be able to get report in PDF format (there are other pdf printer drivers but this one is free and open-source).

Also I know HTML is not as flexible as PDF but it might be good enough. I was doing some tests with real users and they actually like it because not only they can now print anything to PDF (even beyond my app), also their workflow is faster because they don't have to download and wait until their pdf reader opens up. they just print (or export to pdf) what they see on website directly from their webbrowser... kind of makes sense.

(HTML + CCS3 Generated Content for Paged Media) to PDF?

They only application I know of that can possibly convert HTML/CSS3 to good quality PDF is PrinceXML. (And it is my impression, that Google licensed and utilizes their code somehow in GoogleDocs for PDF export.)

However, this is a commercial application, and I think it comes with a hefty price tag.

One of it's leading lights is Håkon Lie, who is also behind all the technical achievements of the Opera web browser.

Here is a 2 years old Google Tech Talks video where Lie outlines some of the technologie behind PrinceXML:

  • http://www.youtube.com/watch?v=vcXUrNSvjhU

Converting HTML files to PDF

The Flying Saucer XHTML renderer project has support for outputting XHTML to PDF. Have a look at an example here.



Related Topics



Leave a reply



Submit