简体   繁体   中英

Convert a HTML/CSS/Javascript file to PDF using Python?

I need to convert a HTML+CSS+Javascript file to PDF from a Django View and therefor using Python.

I could use xhtml2pdf/PISA but it only supports HTML & CSS, not Javascript, which is required.

Another option would be py-wkhtmltox, but it's getting pretty old and I haven't been able to get it to work yet, I just get "ImportError: libwkhtmltox.so.0: cannot open shared object file: No such file or directory", not sure where to get that file other then just to out of the blue rename libwkhtmltox.so to libwkhtmltox.so.0, but then I just get:

Traceback (most recent call last):
  File "pdf_test1.py", line 5, in <module>
    pdf.set_object_setting('path', 'http://www.google.com')
  File "wkhtmltox.pyx", line 118, in wkhtmltox.Pdf.__getattr__ (wkhtmltox.c:1228)
AttributeError: 'wkhtmltox._Pdf' object has no attribute 'set_object_setting'

Yet another option would be to use webkit ( http://bharatikunal.wordpress.com/2010/01/31/converting-html-to-pdf-with-python-and-qt/ ) but I can't execute "sys.exit(app.exec_())" from a Django view.

The only thing I can think of right now is to create a seperate webkit python script and os.system it from the django view, making "sys.exit(app.exec_())" possible and therefor the resultin PDF. But I'm open to other suggestions, seams a bit strange to use os.system from a django view.

Any ideas?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM