2

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?

01AutoMonkey
  • 2,515
  • 4
  • 29
  • 47
  • 3
    Why do you need javascript? Javascript is never going to render in a pdf. If you're using javascript to render style, move it to the css. If you're using javascript to dynamically generate html, you'll need to do that on the server for the purposes of pdf conversion. My suggestion, make the adjustments you need to make it work with xhtml2pdf/PISA. Javascript isn't going to be an option. – Nick Hagianis Apr 19 '12 at 19:40
  • This library (http://code.google.com/p/pyfpdf/) seems to support javascript, I remember doing it in php with some other library and it even displayed alert and so on – Jonathan Liuti Apr 19 '12 at 19:57

1 Answers1

4

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

Simon
  • 12,018
  • 4
  • 34
  • 39