I’m trying to generate reports and return them to requesting client as a file:
client end is quite simple:
$.ajax({
url: '/reports-get/',
data: data_to_submit,
type: 'GET',
statusCode: {
200: function(e){},
500: function(e){}
}
});
The server end is:
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('UTF-8')), result, encoding='UTF-8', link_callback=fetch_resources)
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
result.close()
I’m testing this with Chrome/FF and what happens is:
I see the following response:
Request URL:http://localhost:8000/reports-get/?csrfmiddlewaretoken=e70579b0ad62d3744133dec4d51c98cc&delivery_type=pdf&report_name=consolidated_report&group_by=managers&projects_to_display=all
Request Method:GET
Status Code:200 OK
and response header:
Content-Disposition:attachment; filename=consolidated report 2012-04-03 grouping by managers2012-04-03-02-02.pdf
Content-Encoding:gzip
Content-Length:83312
Content-Type:application/pdf
Date:Mon, 02 Apr 2012 22:02:48 GMT
Server:WSGIServer/0.1 Python/2.7.1
Vary:Accept-Encoding, Cookie
and the file in response body:
%PDF-1.4
%���� ReportLab Generated PDF document http://www.reportlab.com
% 'BasicFonts': class PDFDictionary
1 0 obj
...
startxref
184036
%%EOF
but it doesn’t save the file neither prompts me if I want to. Is there something I’m missing?
As soon as you’re requesting it programmatically, the browser steps out of the way, or you’d get all sorts of nasty behaviour. One way to make it treat that as a download would be to formulate the URL you need and then set
window.locationto it.