I’m using django and reportlab tool generate Dynamic PDF reports. All the data in report is coming from database.
I’m generating dynamic PDF report using reportlab which consist of the data from database. My problem is whenever I restart Apache server my PDF reports data is changing. The generated report uses Django queries to display the data. But when I restart apache server then the correct data is not appearing in report.
I checked all the queries which I written in my django views. Also I noticed that every restart of Apache server showing different results. So I don’t think so that this is the problem of my django queries. Is there any solution for this problem?
If I restart the apache server data of already generated reports will change, what is the cause for this problem, any solutions?
Or it is due to apache server?
Here is the source code. I think StringIO is wrongly placed.
buffer = StringIO()
def analysis_report(request, sample_no, contact_person):
"""
This function generates the analysis report by using the
client information, transformer details, sample data and
test results of respective analysis.
"""
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment;filename=ANALYSIS_REPORT.pdf'
doc = SimpleDocTemplate(buffer)
document = []
doc.sample_no = sample_no
doc.contact_person = contact_person
image = Paragraph(stamp_image, STYLES['NORMAL_RIGHT'])
document.append(image)
# BUILTIN FUNCTION TO GENERATE THE DOCUMENT.
doc.build(document, onLaterPages=header_footer)
pdf = buffer.getvalue()
response.write(pdf)
return response
Thanks in advance
Firstly, a huge problem in your case may be that
buffer = StringIO()is not in function scope but you use it within your function.I’m not sure what exactly you are doing – the code you’ve given us is the tiniest snippet of a whole working PDF – but here are a couple pointers from a guy that just had to do 2 really huge PDF packages for a django project:
onPage=each of those callbacks should also be defined separately in your classSo here is an example views.py:
And an example of my Pdf Class:
Note: I haven’t tested that class as it is, I basically grabbed a lot of application specific info out of a class that I already had and pared it down for easy digest. It’s meant to give you an idea of how I set up PDFs as a best practice.