[reportlab-users] Save PDF File to model using two views- Solution

Asif Jamadar asif.jamadar at rezayat.net
Sat Jul 2 01:22:30 EDT 2011



Here is the solution,


response = analysis_report(request)

report = ContentFile(response.content)

m = Report()

m.file.save('analysis_report.pdf', report)



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

I was just missing one thing that is 'response.content'

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

Message: 1
Date: Tue, 28 Jun 2011 13:42:01 +0000
From: Asif Jamadar <asif.jamadar at rezayat.net>
Subject: [reportlab-users] Save PDF File to model using two views
To: "reportlab-users at lists2.reportlab.com"
<reportlab-users at lists2.reportlab.com>
Message-ID:
<CC0CC2862BF94D41A65DC4511A12B49328FD0B00 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.



The below code works fine for me



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

This code will saves the PDF to separate model. Here the model is 'Report'. This works fine for me.

But problem is when I use different view to save the report into model. Here is the code which gives me a error


def analysis_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()



response.write(pdf)



return response





def save_report(request)


report = analysis_report(request)

myfile = ContentFile(report)

m = Report()

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

m.report_name = "Analysis Report"

m.save()

return render_to_response(sometemplate.html)


This will throws me error "expected read buffer, HttpResponse found"

However both views should return response



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20110628/4980cd7f/attachment-0001.html>

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

Message: 2
Date: Tue, 28 Jun 2011 14:46:08 +0100
From: Andy Robinson <andy at reportlab.com>
Subject: Re: [reportlab-users] Save PDF File to model using two views
To: reportlab-users <reportlab-users at lists2.reportlab.com>
Message-ID: <BANLkTinMHkSso2w4JCRvMv17L6y3SoDang at mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On 28 June 2011 14:42, Asif Jamadar <asif.jamadar at rezayat.net> wrote:

> How can I save the dynamically generated PDF report into separate django

> model (database) as file.

>


Hopefully someone here can point out a suggestion (I can't look in
detail right now) but in general I think these questions are much more
about Django than about ReportLab. If you don't get a quick answer
you might find the Django lists more useful...

- Andy


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

Message: 3
Date: Tue, 28 Jun 2011 15:02:33 +0100
From: Robin Becker <robin at reportlab.com>
Subject: Re: [reportlab-users] Save PDF File to model using two views
To: reportlab-users <reportlab-users at lists2.reportlab.com>
Message-ID: <4E09DEF9.2060509 at chamonix.reportlab.co.uk>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 28/06/2011 14:42, Asif Jamadar wrote:

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

>

>

>

> The below code works fine for me

>


Hi it looks to me as though analysis_report is producing a response into which
you have written a PDF. That is not the same as writing it into a StringIO
object. So in the first OK view you did

pdf = buffer.getvalue() #produces a stringof bytes
myfile = ContentFile(pdf)

in the second I see this sequence

pdf = buffer.getvalue()
response.write(pdf)
return response

.....

report = analysis_report(request) #ie the returned response
myfile = ContentFile(report) #not a string of bytes

and presumably the error happens here ie report is a response and not a string.

It may be better to factor out the pdf production bit from the analysis_report
function and use it directly.
--
Robin Becker


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

_______________________________________________
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 16
***********************************************


More information about the reportlab-users mailing list