Use Python to convert HTML to PDF
I’m trying to convert HTML to PDF document in Django without success.
I tried using wkhtmltopdf
0.9.9, but Apache throws an error stating that wkhtmltopdf cannot connect to the server. When I use wkhtmltopdf directly, it works very well and converts HTML to PDF documents.
I’ve also tried using unoconv, but the rendered PDF file doesn’t have any CSS applied. I’ve also tried using xhtml2pdf. I’m facing the same issue again; The rendered PDF file does not have any CSS styles applied. I spent most of today and last night trying to fix this, but I’m nowhere near solving it.
Please let me know if you need more information
Solution
Configuring Pisa for Django should not be too hard .
There are indeed a few examples online that show you how to do it,
Explains how to link to external resources in a template:
- http://www.arnebrodowski.de/blog/501-Pisa-and-Reportlab-pitfalls.html
- django – pisa : adding images to PDF output
- http://antydba.blogspot.com/2009/12/django-pisa-polskie-czcionki.html
- http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html
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 versions of Django, you should probably use STATIC_ROOT
instead of MEDIA_ROOT
Then use Get Resources
accordingly in your rendering method:
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")),
result,
link_callback=fetch_resources,
encoding="utf-8")