django-wkhtmltopdf on a linux (Webfaction) server

Generating PDF with python is not an easy thing. Here is what I’ve tried:

  • xhtml2pdf: the worst. It has a very bad CSS interpreter and it is pretty impossible to make a clean PDF out from a complex html page.
  • WeasyPrint: that’s better. It has its own HTML and CSS2 interpreters, and despite some minor issues it is fairly reliable. However it requires some big dependencies, and I eventually had some memory issues on my webserver.
  • wkhtmltopdf with its Django wrapper: for now, the best. It makes use of Webkit to render HTML+CSS+JS(!) pages, I don’t think you can do better. However, it requires QT, and this is big. Well, not the “classical” QT, a patched QT (that’s even worse) that allows to run without an X server. This is what I’m using for now in my projects

We’ll see here how to build wkhtmltopdf and a basic django-wkhtmltopdf usage. These steps has been tested on a Webfaction server (CentOS 6) through SSH.

(more…)

Django – UnicodeDecodeError using i18n

If you’re using i18n in your django website, you may encounter this issue. It took me two hours to find where the problem came from: you have to use ugettext instead of gettext if your translated strings contains non-ascii characters.

(more…)

Django – Absolute media URIs from templates

Sometimes, you need to build an absolute URI to a media file from your templates:

1
http://example.com/media/sample.jpg

After following this small tutorial, you’ll be able to do this:

1
2
{% load mytemplatetags %}
{{ media_name|absolute_media_url:request }}

(more…)