且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python将HTML / CSS / Javascript文件转换为PDF?

更新时间:2022-01-15 09:53:50

我建议使用 wkhtmltopdf 。它可以作为linux的静态二进制文件使用,因此您不应该遇到任何共享库问题。 wkhtmltopdf包装了webkit并支持javascript,您甚至可以告诉它在呈现之前运行任何JS代码多长时间。我已经成功地使用它来制作页面的PDF,这些页面上有一些复杂的图表生成代码。只需使用 subprocess.call()启动它。

I suggest using wkhtmltopdf. It's available as a static binary for linux, so you shouldn't run into any shared object problems. wkhtmltopdf wraps webkit and supports javascript, and you can even tell it for how long to run any JS code before the rendering takes place. I've used it successfully to make PDFs of pages that had some complicated chart generating code on them. Just launch it using subprocess.call().