10

There are a lot of different ways of generating pdfs from a django webpage in python2. The most clean, probably, is pisa and reportlab. These do not work for python3 though.

So far, the only method I've had success with is to render the template, write it to a file, and then use wkhtmltopdf via subprocess.popen. This works alright, but it doesn't load any of my static files, such as css and images.

Are there any proper solutions? can wkhtmltopdf read my staticfiles from the command line, in some way, or is there a library like pisa/reportlab, which supports python3?

I haven't been able to finde such a library

4 Answers 4

13

You could use Weasyprint. You could easily render directly.

You could do something like that:

    html = HTML(string=htmlstring)
    main_doc = html.render()
    pdf = main_doc.write_pdf()
    return HttpResponse(pdf, content_type='application/pdf')

To render your Django view to HTML, you could simply use the shortcut render_to_string(self.template_name, context, context_instance=RequestContext(self.request))

Be aware, when using This with a Synchronous Webserver/WSGI Server ALL requests will be blocked until the PDF is rendered. So consider using an ASYNC Worker.

2
  • Thank you for this! It did not show up in any of my searches, but it works perfectly. Installing it took a little effort, since it needs a handful of non-python libraries, but I managed. Thanks a bunch
    – Eldamir
    Commented Nov 20, 2014 at 10:17
  • I tried to use Weasyprint and it works with Python3 but it consumes a lot of memory - it takes 500M to generate PDF from less then 1M html.
    – asafm
    Commented Nov 22, 2017 at 14:02
6

I looked into Weasyprint, wkhtmltopdf and even LaTeX, but all have external binary dependencies that are difficult to deploy to services such as Heroku.

The best combination I found so far that works in Django on Python 3 is using Reportlab (now works on Python 3) + xhtml2pdf. xhtml2pdf only recently added beta Python 3 support so you need to install it with:

pip install --pre xhtml2pdf

If you have these two installed, you can either use xhtml2pdf directly or install the django-easy-pdf package which provides a TemplateView to inherit from and an example base template & styling to get you started quickly. Follow their quickstart instructions and you can quickly prepare something like a detail view that renders to PDF like:

class InvoicePDFView(PDFTemplateView):
    template_name = "invoice_pdf.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        myinstance = get_object_or_404(MyModel, pk=context['pk'])
        context['myinstance'] = myinstance
        return context

And in your urls.py you'd add something like:

url(r'invoice/(?P<pk>[^/]+)/$', InvoicePDFView.as_view(), name='invoice')
0

wkhtmltopdf requires

  • CSS to be inlined
  • static files to be locally present on the server
  • static file urls to be os paths, eg: /home/ubuntu/project/project/static/file_name
0

I have tried pydf in Python3, its working fine.

pip install python-pdf

For python2 use pip install python-pdf==0.30.0

Documentation https://github.com/tutorcruncher/pydf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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