Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 (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 Answer

up vote 1 down vote accepted

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

share|improve this answer
Just a heads up, be wary of django-wkhtmltopdf if you are deploying it on Apache github.com/incuna/django-wkhtmltopdf/issues/15 – krammark23 Jan 16 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.