[Zope] PDF from external method
Jim Washington
jwashin@vt.edu
Thu, 25 Oct 2001 11:52:45 -0400
Jim Penny wrote:
> On Thu, Oct 25, 2001 at 09:23:59AM -0400, Jim Washington wrote:
>
>>Juergen R. Plasser / HEXAGON wrote:
>>
>>
>>>Ok, sorry, this was a python script error.
>>>
>>>Another question: Reportlab examples create files, how can I "RESPONSE"
>>>them to Zope?
>>>
>>What I am using:
>>
>>pdffile.seek(0)
>>self.REQUEST.RESPONSE.setHeader("Content-type","application/pdf")
>>self.REQUEST.RESPONSE.write(pdffile.read())
>>
>>-- Jim Washington
>>
>>
>
> This is very nice.
>
> Yet another way is to simply build a HTML file pointing to the
> .pdf file generated.
>
> I.e.
> def build_pdf(...):
> ...
> # omitted code builds pdf file in variable pdf_filename
> # referenced as pdf_url
> return "<a href="%s">%s</a>" % (pdf_url, pdf_filename)
>
> Advantages:
> Allows .pdf to be served by static webserver
> Disadvantages:
> Requires extra click to get to pdf.
> Requires a policy to reap stale pdf's
> Neutral:
> Caching generated pdf's may (or may not) be a big win. It depends
> on the nature of your task. I suspect, psychobabblically speaking,
> that caching will occur more naturally to people who are treating
> pdf's as semi-static content by delivering a file name, than to
> those who purely consider a pdf to be something to be delivered
> as a result of a method invocation.
>
> Note: The disadvantages may not be as large as you think. Many
> people are disconcerted by long load-times and/or sudden pop-up
> windows. This splits download/viewer startup time away from
> data gathering/pdf generation time and will typically
> be perceived as more responsive; despite stop watch time showing
> that it is far slower due to the necessity of the extra click.
> (That is, people are often happier with 2 events of duration t/2
> than one event of duration t, especially if they have a chance to
> "walk away" in the middle.) Thus, for many people, the extra click
> may be perceived as an advantage!
>
> The policy for reaping pdf's is actually neutral. Note that Jim
> Washington's method left the pdf file on the hard disk somewhere.
> If you regard the external method as generating files rather than
> byte-streams, the need for a reaping process simply leaps out at you.
>
> Jim Penny
Thanks, Jim. I may use some of this. I am currently working on the
user interface for a multitude of reports.
A small caveat to the above. The PDF's I am generating are small,
150-200k, and not particularly high-traffic.
I create the PDFs entirely in memory, so I do not need to reap:
from cStringIO import StringIO
pdffile = StringIO()
doc = BaseDocTemplate(pdffile,
pageTemplates=([coverTemplate,secondPageTemplate, mainTemplate,]))
doclist = []
...use platypus...
doc.build(doclist)
then write to RESPONSE as in my note above.
-- Jim Washington