FOSS HTML to PDF in Python, .Net or command line?

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have google as much as I possible, checked stackoverflow several times, and yet I can not find a good html to pdf converter that can handle css. Is there a free and open source solution (even for commercial usage)? There are many solutions, with huge variety of price ranges, but I was looking for something open source and free. I have tried PISA for Python and it works fairly well, but is not free for commercial usage. Is there anything for .Net? I have not had success with iTextSharp.

回答1:

I've wkhtmltopdf used on a couple of projects. http://code.google.com/p/wkhtmltopdf/. It uses the webkit rendering engine, which powers the Safari browser. You'll get completely up to date rendering just like a web browser with CSS and all.

Oh, and it's open source.



回答2:

I haven't found a good FOSS solution, but I can say that PrinceXML works very well, provides quite a bit of functionality through the command-line and is priced very reasonably. IIRC, the free version appends a cover page to each PDF you produce, which may or may not be a deal-killer for you, but you should definitely check it out.



回答3:

The fact that you're asking about C#/.NET makes me guess you're on a Windows platform, so this may not work for you, but I've had decent success using html2ps in conjunction with ps2pdf:

#! /bin/sh  TEMPDIR="/tmp" html2ps -u $1 > $TEMPDIR'temp.ps' ps2pdf $TEMPDIR'temp.ps' $2 

That does handle CSS (at least all the CSS I've thrown at it so far) though there are some limitations if I remember right with regards to some in-line styles.

NOTE: I think these are both available on Windows via Cygwin, if that's an acceptable solution for you.



回答4:

You can also try different approach like using virtual printers.



回答5:

I have had stunning success using the 'PISA' xhtml2pdf tool found here: http://www.xhtml2pdf.com/

Don't be scared off by the 'xhtml' part, it accepts a large range of html input, and produces PDFs according to CSS, including CSS print extensions.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!