Creating PDF views

When working with ReportLab, we briefly mentioned that we can create canvas or document templates using either a file name or an open Python file, or file-like, object. In our previous examples, we created a new file using a file name passed as a string to our ReportLab constructors. If we wanted, we could have opened a file manually and used it as a constructor argument:

f = open('products.pdf')
doc = SimpleDocTemplate(f)

This is important because when working with Django views, we want to be able to return this PDF file after generating it from our database information. One idea is to generate the file on disk and redirect the user's browser to the newly create PDF. But this is a lot of extra effort and will require maintenance of the disk to prevent old report requests from using up our server's storage space.

A much better alternative is to use ReportLab's support for file-like objects with our Django response object. Django views always return an instance of the HttpResponse class. It turns out that this response object is itself a file-like object; it supports a write method and can therefore be used with our ReportLab routines. This works like so: our view code constructs a response object and passes it to our report generation code. When our report is created, control returns to our view, which returns our response object that now contains the report.

Here is an example Django view that generates a pie chart using the routine listed in the previous section. The pie chart code has been moved to a make_pie_chart function, which takes and returns a file object:

def piechart_view(request):
    	response = HttpResponse(mimetype='application/pdf') 
doc = SimpleDocTemplate(response)
Catalog = [] 
style = styles['Normal'] 
p = Paragraph("Cranberry Sauce Sales", styles['Heading1']) 
Catalog.append(p) 
d = Drawing(100, 125) 
cht = Pie() 
cht.data = sales[0]
cht.labels = months
cht.slices[0].popout = 10
d.add(cht)
Catalog.append(d)
doc.build(Catalog)
return response

The key to these specialized views is setting the appropriate mimetype on the HttpResponse object. Browsers will interpret the mimetype and launch the appropriate in-browser handler for the file returned by the view. You can specify other mimetypes in this way, as well. If we were using ReportLab to generate a PNG file instead of PDF, we would use this:

response = HttpResponse(mimetype='image/png')

This is an extremely powerful feature of Django and allows us to write views that generate reports based on parameters defined in URL patterns or using GET and POST values. You could even manipulate the report's display by processing GET parameters and adjusting the resulting report (for example, breaking out the smallest element of the pie chart if a certain GET flag is included).

If we wanted to use a toolkit besides ReportLab, the implementation pattern would be exactly the same. Anything that can work with Python file-like objects can be used in a view, as we've done previously. We can generate any type of file, even spreadsheet data in a comma-separated value format, as long as we provide the appropriate mimetype.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset