I am trying to convert HTML into a PDF document in Django and haven\'t been successful.
I have tried using wkhtmltopdf 0.9.9, however Apache throws an error that wkh
I suggest you to use pisa, pypdf and html5lib combination, it worked for me.
Configuring Pisa for Django shouldn't be too hard.
There are really several examples on the net that show you how to do it and explain how to link to external resources in your templates:
In your case you should try the link-callback-function mentioned in the first blog post:
def fetch_resources(uri, rel):
"""
Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc.
`uri` is the href attribute from the html link element.
`rel` gives a relative path, but it's not used here.
"""
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
return path
For newer Django-Version you probably should use STATIC_ROOT
instead of MEDIA_ROOT
Then use fetch resources
accordingly in your render-method:
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")),
result,
link_callback=fetch_resources,
encoding="utf-8")
A possible, but not so elegant solution, is to run a small scripts which renders the html via a headless browser component (webkit/xvfb on Linux) and then saves it as a pdf.