[reportlab-users] Return a file object rather than save afile...

Roberto Alsina ralsina at netmanagers.com.ar
Fri Oct 2 08:10:48 EDT 2009


On Friday 02 October 2009 08:43:37 Andy Robinson wrote:

> 2009/10/2 Brower Jason <Jason.Brower at kone.com>:

> > Somehow I need to make the save work with the output and I am am just to

> > know with this file object stuff to know what to do.

>

> Here's how to save your PDF as a string in memory.

>

> buf = StringIO.StringIO()

>

> c = Canvas(buf)

>

>

> #do your drawing here

>

>

> c.save() #PDF file is written into the buffer at this point

>

>

> #extract contents as a byte string

> buf.reset()

> rawPdfFileContent = buf.read()

>

> You then pass this to the response. If it was Django, I could write

> to the response object (which is file-like), so I could do this and

> cut out the StringIO...

>

> response.setContentType('application/pdf')

> c = Canvas(response)

> ....

> c.save()


Isn't this faster?

buf = cStringIO.StringIO()
c=Canvas(buf)
#do your drawing
c.save()
rawPdfFileContent = buf.getvalue()

--
("\''/").__..-''"`-. . Roberto Alsina
`9_ 9 ) `-. ( ).`-._.`) KDE Developer (MFCH)
(_Y_.)' ._ ) `._`. " -.-' http://lateral.netmanagers.com.ar
_..`-'_..-_/ /-'_.' The 6,855th most popular site of Slovenia
(l)-'' ((i).' ((!.' according to alexa.com (27/5/2007)


More information about the reportlab-users mailing list