[Zope] Generating and Downloading PDF
Chris McDonough
chrism at plope.com
Fri Feb 25 23:28:25 EST 2005
On Fri, 2005-02-25 at 21:24, Paul Winkler wrote:
> On Sat, Feb 26, 2005 at 12:14:11AM +0100, robert wrote:
>
> > R = self.REQUEST.RESPONSE
> > R.setHeader('content-type', 'application/rtf')
> > R.setHeader('content-length', str(len(data))
> > R.write(data)
>
> That's great if you're serving PDFs that
> you can easily fit in RAM and throw away after the request.
> e.g something customized for each viewer.
>
> Or, if the PDFs are likely to be large, and you can
> reuse them, and you have space on the filesystem for them,
> and you want the fastest possible data stream
> Zope can deliver to the client while minimizing RAM
> usage, you could do something like (for zope later than 2.7.2 or so,
> I forget exactly which version):
>
>
> # untested!
> # doesn't provide cleanup of files!
>
> from ZPublisher.Iterators import filestream_iterator
> response = self.REQUEST.RESPONSE
> if not file_i_want_to_serve_exists:
> # ... write the file somewhere.
> # Bonus points if you can figure out how to start streaming it
> # while you're still writing it, but that may be
> # non-trivial.
>
> # Once the file's written, hand it off to the publisher
> # for efficient streaming.
> response.setHeader('content-type', 'application/pdf')
> response.setHeader('content-length', the_length)
> response.setBody(filestream_iterator(filename))
Actuall, it's not "response.setBody" in the last line, it's just "return
filestream_iterator(filename)".
- C
More information about the Zope
mailing list