[Zope] Generating and Downloading PDF

Paul Winkler pw_lists at slinkp.com
Fri Feb 25 21:24:34 EST 2005


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)) 

-- 

Paul Winkler
http://www.slinkp.com


More information about the Zope mailing list