[mod_python] ReportLab integration

Colin Bean ccbean at gmail.com
Mon Oct 16 15:44:03 EDT 2006


Hi Jim,

So you're just getting a blank page in IE when you try to access your
pdf?  One case is that the content is actually being sent to IE, and
IE isn't displaying to properly, another is the content isn't being
sent correctly / completely.  I'm guessing it's the first, since
there's not much that's browser-dependent in your code, but you might
want to check this.  In the past, I"ve used a lightweight http proxy
(pick your flavor -- webscarab worked well for me) between browser and
server, to make sure that the complete content is actually being sent
to the browser.

So, assuming the pdf content is actually being send but IE won't display it:
Just realized that the microsoft KB article never mentions this
(sorry), but if you change "content-disposition: attachment..." to
"content-disposition: inline...", it will set the filename / type for
inline display like you're looking for.

Here's another snippet that talks about handling pdf content (it's for
java, but mainly deals with headers / filename stuff):
http://www.rgagnon.com/javadetails/java-0404.html

hth,
Colin



On 10/16/06, Jim Steil <jim at qlf.com> wrote:
>
>
>
>
> It runs my script, but doesn't go anywhere when it is done.  The code looks
> like this:
>
>
>
> ---------------------
>
> # Create the document.
>
> reportPeriod = 'Report for the Month of %s %s' % (months[reportMonth],
> reportYear)
>
> output = StringIO.StringIO()
>
> doc = SimpleDocTemplate(output, pagesize=landscape(letter))
>
> doc.m = reportPeriod
>
> go(doc, reportMonth, reportYear)
>
> # Return the rendered page content.
>
> content = output.getvalue()
>
>
>
> req.content_type = "application/pdf"
>
> req.headers_out['Content-Length'] = str(len(content))
>
>
>
> # Internet Explorer chokes on this with PDFs.
>
> #req.headers_out['Pragma'] = 'no-cache'
>
> #req.headers_out['Cache-Control'] = 'no-cache'
>
> #req.headers_out['Expires'] = '-1'
>
>
>
> req.send_http_header()
>
> req.write(content)
>
> return apache.OK
>
> ---------------------
>
>
>
>
>
> This works fine in Firefox, but doesn't do anything in IE.  The comment in
> the code about Internet Explorer choking was there in Grahams example.
>
>
>
> I looked at the link you provided, but it seems to speak about doing a Save
> As when you display the content.  That is not what I'm trying to accomplish.
>  I want the pdf to come back to the screen, but have it come back with a
> name.
>
>
>
>       -Jim
>
>
>
> Jim Steil
>
> IT Manager
>
> Quality Liquid Feeds
>
> (608) 935-2345
>
> (608) 341-9896 cell
>
>
>
>
> -----Original Message-----
>  From: Colin Bean [mailto:ccbean at gmail.com]
>  Sent: Monday, October 16, 2006 2:05 PM
>  To: Jim Steil
>  Cc: mod_python at modpython.org
>  Subject: Re: [mod_python] ReportLab integration
>
>
>
> Hi Jim,
>
>
>
> How does it break in IE?  If it it won't recognize the result is a
>
> pdf, try setting the content-disposition header.  You can use this to
>
> specify a filename that browser will save your file as; if you give it
>
> a .pdf extension then IE should open it fine.  Here's a little more
>
> info on it:
>
> http://support.microsoft.com/kb/260519
>
>
>
> I *think* that changing your handler to use a .pdf extension will do
>
> the same thing (I haven't used publisher yet so I'm not sure how to do
>
> this); the content disposition trick might be easier, though.
>
>
>
> -Colin
>
>
>
> On 10/16/06, Jim Steil <jim at qlf.com> wrote:
>
> >
>
> >
>
> >
>
> >
>
> > Graham:
>
> >
>
> >
>
> >
>
> > Thanks so much for the examples.  That's exactly what I needed.  I used
> the
>
> > example in Reportlab-demo.py and it is working great in FireFox, but not
> in
>
> > IE.  There is a comment in the source about IE choking on some code but I
>
> > don't know how to get around that.  Also, is there some trick or
>
> > configuration I need to do to get a .pdf extension on my url?  I'm using
> the
>
> > publisher handler.
>
> >
>
> >
>
> >
>
> > Thanks again.  What I've got working now really helps, and getting around
>
> > these 2 issues would just be icing on the cake!
>
> >
>
> >
>
> >
>
> >             -Jim
>
> >
>
> >
>
> >
>
> >
>
> > Jim Steil
>
> >  IT Manager
>
> >  Quality Liquid Feeds
>
> >  (608) 935-2345
>
> >
>
> > (608) 341-9896 cell
>
> >
>
> >  ________________________________
>
> >
>
> >
>
> > From: Graham Dumpleton [mailto:grahamd at dscpl.com.au]
>
> >  Sent: Saturday, October 14, 2006 12:54 AM
>
> >  To: Jim Steil
>
> >  Cc: mod_python at modpython.org
>
> >  Subject: Re: [mod_python] ReportLab integration
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > On 14/10/2006, at 6:25 AM, Jim Steil wrote:
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > Can someone help get me pointed in the right direction?  I want to allow a
>
> > user to request a PDF from my mod_python app.  When they do so, I want to
>
> > generate the PDF on the fly with ReportLab and then send the resulting PDF
>
> > back to the browser without saving it anywhere.  I've done some searches
> on
>
> > the web but can't seem to find examples of doing this with just mod_python
>
> > and ReportLab.  I've seen examples of using other tools, but not just
>
> > mod_python.  This sounds like something others are probably doing, but I
>
> > need a push in the right direction.  Thanks in advance for your time.
>
> >
>
> > Try:
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
> http://www.dscpl.com.au/projects/vampire/examples/reportlab/reportlab-demo.pdf
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > This uses reportlab to generate the PDF.
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > There is also:
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
> http://www.dscpl.com.au/projects/vampire/examples/reportlab/trml2pdf-demo.pdf
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > This uses free version of tool for converting reportlab RML to PDF.
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > The actual source code for each is at:
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
> http://www.dscpl.com.au/projects/vampire/source/examples/reportlab/reportlab-demo.py
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
>
> >
> http://www.dscpl.com.au/projects/vampire/source/examples/reportlab/_handler.py
>
> >
>
> >
>
> >
>
> >
> http://www.dscpl.com.au/projects/vampire/source/examples/reportlab/trml2pdf-demo.rml
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > These happen to use Vampire for the mod_python handler framework,
>
> >
>
> >
>
> > but you should be able to adapt them to other mod_python handler
>
> >
>
> >
>
> > systems.
>
> >
>
> >
>
> >
>
> >
>
> >
>
> > Graham
>
> > _______________________________________________
>
> > Mod_python mailing list
>
> > Mod_python at modpython.org
>
> > http://mailman.modpython.org/mailman/listinfo/mod_python
>
> >
>
> >
>
> >


More information about the Mod_python mailing list