Monday 6 October 2014

PDF generation in Django Apps

One of the beautiful thing when using Django is the rich set of application ready to use as a plugin in your project. Is it use at is or modify/extend with a little effort.

One of my project need to generate a PDF and the time is come to get some Django PDF application plugin. So Django documentation officially told to use reportlab. I try the reportlab, but got error with first try from the code in Django documentation stated here.

Then look further, i found pisa is very neat to generate a PDF for you with a common Django way. How does pisa do the job? Well it still need reportlab, and html5lib , and it just convert your HTML output into pdf output using your django view.

So we already have the html output in Django app, then just feed it to pisa, and the pdf will generated.
You can download pisa3.0.33 in here .

But also notice that pisa3.0.33 is got problem which cause it cannot run with reportlab>3.0 .

To fix it go to the installation of pisa , and find sx/pisa3/pisa_util.py :

if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):

   raise ImportError("Reportlab Version 2.1+ is needed!")

REPORTLAB22 = (reportlab.Version[0] == "2" and reportlab.Version[2] >= "2")
This will make pisa fail when the import processed by python. To resolve this replace it with this code :
if not (reportlab.Version[:3]>="2.1"):

    raise ImportError("Reportlab Version 2.1+ is needed!")

REPORTLAB22 = (reportlab.Version[:3]>="2.1")

Then that's it, your pisa will work with Django.

The next post , i will give the code to generate the pdf with pisa in django.

Cheers

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More