开发者

Pure python solution to convert XHTML to PDF

开发者 https://www.devze.com 2022-12-09 04:38 出处:网络
I am after a pure Python solution (for the GAE) to convert webpages to pdf. I had a look at reportla开发者_如何学编程b but the documentation focuses on generating pdfs from scratch, rather than conve

I am after a pure Python solution (for the GAE) to convert webpages to pdf.

I had a look at reportla开发者_如何学编程b but the documentation focuses on generating pdfs from scratch, rather than converting from HTML.

What do you recommend? - pisa?

Edit: My use case is I have a HTML report that I want to make available in PDF too. I will make updates to this report structure so I don't want to maintain a separate PDF version, but (hopefully) convert automatically.

Also because I generate the report HTML I can ensure it is well formed XHTML to make the PDF conversion easier.


Pisa claims to support what I want to do:

pisa is a html2pdf converter using the ReportLab Toolkit, the HTML5lib and pyPdf. It supports HTML 5 and CSS 2.1 (and some of CSS 3). It is completely written in pure Python so it is platform independent. The main benefit of this tool that a user with Web skills like HTML and CSS is able to generate PDF templates very quickly without learning new technologies. Easy integration into Python frameworks like CherryPy, KID Templating, TurboGears, Django, Zope, Plone, Google AppEngine (GAE) etc.

So I will investigate it further


Have you considered pyPdf? I doubt it has anywhere like the functional richness you require, but, it IS a start, and is in pure Python. The PdfFileWriter class would be the one to generate PDF output, unfortunately it requires PageObject instances and doesn't provide real ways to put those together, except extracting them from existing PDF documents. Unfortunately all richer pdf page-generation packages I can find do appear to depend on reportlab or other non-pure-Python libraries:-(.


What you're asking for is a pure Python HTML renderer, which is a big task to say the least ('real' renderers like webkit are the product of thousands of hours of work). As far as I'm aware, there aren't any.

Instead of looking for an HTML to PDF converter, what I'd suggest is building your report in a format that's easily converted to both - for example, you could build it as a DOM (a set of linked objects), and write converters for both HTML and PDF output. This is a much more limited problem than converting HTML to PDF, and hence much easier to implement.

0

精彩评论

暂无评论...
验证码 换一张
取 消