[reportlab-users] Image in a Python page
Tim Roberts
timr at probo.com
Mon Jun 4 14:20:33 EDT 2007
David Frank wrote:
> Hi All,
>
> This might not probably be the right place to post this question. But,
> I was wondering if this problem was maybe because of ReportLab. I am
> creating an image dynamically and I am using python in cgi to display
> that image. I have Windows and I am running Apache 2.2 with Python 2.4
> and Reportlab 2.1. This is my code for the python file, newbars.py and
> for the image Newbars.gif. The major parts are in normal font and the
> rest are in italics.
>
> #!C:/Python24/python
> print "Content-type: text/html\n\n"
> ...
> renderPM.drawToFile(drawing, "Newbars.gif", "GIF")
> print "Content-type: image/gif"
> print 'Image Created'
> print '<html><body><img src = "Newbars.gif"></img></body></html>'
Andy gave you some good advice on the "special" nature of the cgi-bin
directory, but there was something else here I wanted to point out.
Notice that you have a "Content-Type" header at the beginning with a
blank line. After that, you can't issue any more headers. Everything
else is just content. So, the "image/gif" line will be seen as just
another line of HTML. It IS possible to have a CGI script return a GIF
image, by changing the very first line to "Content-Type: image/gif\n"
and then printing the GIF data as a unit.
Also note that you don't want TWO \n characters in that first line.
Remember that there is a newline at the end of every print statement.
Thus, you are getting TWO blank lines after the header. That's not a
problem for text/html, but it will fail for image/gif.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the reportlab-users
mailing list