Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML into PDF using Python

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 wkhtmltopdf cannot connect to server. When I use wkhtmltopdf directly, it runs perfectly fine and converts the HTML into a PDF document.

I have also tried using unoconv, however the rendered PDF file doesn't have any CSS applied to it. I have also tried using xhtml2pdf. Again I am facing same issue; the rendered PDF file doesn't have any CSS styling applied. I have spent the better part of today and last night trying to solve this issue and I'm still no closer to solving the problem.

Let me know if you need any more information

like image 846
Ritesh Kadmawala Avatar asked Oct 28 '25 12:10

Ritesh Kadmawala


1 Answers

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:

  • 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 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")
like image 68
arie Avatar answered Oct 30 '25 03:10

arie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!