[reportlab-users] Saving the generated report into Django model

Asif Jamadar asif.jamadar at rezayat.net
Thu Jun 23 01:14:03 EDT 2011


I found the solution,

def report(request)

response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment;filename=ANALYSIS_REPORT.pdf'
buffer = StringIO()

doc = SimpleDocTemplate(buffer)

#Create the PDF


doc.build(document, onLaterPages=header_footer)

pdf = buffer.getvalue()

myfile = ContentFile(pdf)

m = Report()

m.file.save('test.pdf', myfile)

response.write(pdf)

return response

Here Report is the model and file is the field of Report model.

Hope it will help someone



-----Original Message-----
From: reportlab-users-bounces at lists2.reportlab.com [mailto:reportlab-users-bounces at lists2.reportlab.com] On Behalf Of reportlab-users-request at lists2.reportlab.com
Sent: Wednesday, June 22, 2011 7:00 PM
To: reportlab-users at lists2.reportlab.com
Subject: reportlab-users Digest, Vol 87, Issue 12

Send reportlab-users mailing list submissions to
reportlab-users at lists2.reportlab.com

To subscribe or unsubscribe via the World Wide Web, visit
http://two.pairlist.net/mailman/listinfo/reportlab-users
or, via email, send a message with subject or body 'help' to
reportlab-users-request at lists2.reportlab.com

You can reach the person managing the list at
reportlab-users-owner at lists2.reportlab.com

When replying, please edit your Subject line so it is more specific
than "Re: Contents of reportlab-users digest..."


Today's Topics:

1. how can i save PDF report into django model (Asif Jamadar)
2. Re: how can i save PDF report into django model (Andy Robinson)


----------------------------------------------------------------------

Message: 1
Date: Wed, 22 Jun 2011 05:23:48 +0000
From: Asif Jamadar <asif.jamadar at rezayat.net>
Subject: [reportlab-users] how can i save PDF report into django model
To: "reportlab-users at lists2.reportlab.com"
<reportlab-users at lists2.reportlab.com>
Message-ID:
<CC0CC2862BF94D41A65DC4511A12B49328FCE637 at weaver.REZAYAT.NET>
Content-Type: text/plain; charset="us-ascii"

How can I save the dynamically generated PDF report into separate django model (database) as file.

I created model called 'Reports', in this model I defined a FileField.

In models.py

Class Report(models.Model):

file = models.FileField(upload_to='uploads/documents)'


In views.py

def report(request):

response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment;filename=ANALYSIS_REPORT.pdf'
buffer = StringIO()
doc = SimpleDocTemplate(response)
...................................................................
....................................................................
doc.build(document)
pdf = buffer.getvalue()

myfile = ContentFile(pdf)
m = Report()
m.file.save('test.pdf', myfile)
return response

But the above code saves the EMPTY report. So any advice on how to save the generated PDF report in django model

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20110622/94e9ca56/attachment.htm>

------------------------------

Message: 2
Date: Wed, 22 Jun 2011 08:50:06 +0100
From: Andy Robinson <andy at reportlab.com>
Subject: Re: [reportlab-users] how can i save PDF report into django
model
To: reportlab-users <reportlab-users at lists2.reportlab.com>
Message-ID: <BANLkTimTnS13n41mJaDGofSgQ5=mVKbx_w at mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 22 June 2011 06:23, Asif Jamadar <asif.jamadar at rezayat.net> wrote:

> ???????? response = HttpResponse(mimetype='application/pdf')

>

> ?? ???????????? ?????????response['Content-Disposition'] =

> 'attachment;filename=ANALYSIS_REPORT.pdf'

>

> ??????????????? ??????? buffer = StringIO()

> ??????????????? ?????? doc = SimpleDocTemplate(response)

> ??????????????? ???? doc.build(document)

> ??????????????? ???? pdf = buffer.getvalue()


It looks from this like you are not actually using the 'buffer' variable. Try
doc.build(document, filename=buffer)

Originally we allowed a filename, but later we generalised it to allow
a file-like object.

- Andy

------------------------------

_______________________________________________
reportlab-users mailing list
reportlab-users at lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users


End of reportlab-users Digest, Vol 87, Issue 12
***********************************************


More information about the reportlab-users mailing list