[reportlab-users] Appengine - Reportlab (Get Photo from Model)

Martinho Arantes martinhoarantes at gmail.com
Tue Oct 5 03:51:57 EDT 2010

Thanks for all your reply,
Let me say that i changed utils.py, like Nick Johnson (Google) suggested in
his blog:

Here´s what he posted
Images in PDFs

It's also quite possible to insert images in a PDF, of course, and ReportLab
supports doing so in a limited fashion without requiring access to the PIL
imaging library. Unfortunately, a couple of issues - one incompatibility and
one outright bug - require us to make a couple of small patches to the
library before we can use it with images.

First, open up reportlab/lib/utils.py, and go to the rl_isdir() function,
starting on line 463. This function depends on some internals of the Python
classloader that aren't available on App Engine, so we need to change it.
Comment out the last line, line 469, and in its place, insert "return

Next, look at the _isPILImage() function, starting on line 520. Line 523
reads "except ImportError:". Change this to read "except AttributeError:".

Now that we've made these modifications, we can insert images in our PDFs!
The PDF format effectively supports two types of image - raw image data
(optionally compressed with zlib), and JPEGs. As it stands, the ReportLab
library only exposes the latter to users, so we'll only be able to include
JPEGs for now. If your images are in another format, you can use the Images
service <http://code.google.com/appengine/docs/python/images/> to convert
them to JPEG.

In order to insert an image into a PDF, we first have to create an
ImageReader object from the JPEG data. Then, we call the canvas's
drawImage() method to actually draw it to the PDF. Here's an example:

image = canvas.ImageReader(StringIO.StringIO(image_data)) # image_data
is a raw string containing a JPEG

c.drawImage(image, 0, 0, 144, 144) # Draw it in the bottom left, 2
inches high and 2 inches wide

2010/10/5 Andy Robinson <andy at reportlab.com>

> On 5 October 2010 08:20, Henning von Bargen <H.vonBargen at t-p.com> wrote:

> > The following code in utils.py is bad coding style IMHO

> > as it hides the actual cause and results in a possibly misleading

> message:

> >

> > from reportlab.pdfbase.pdfutils import readJPEGInfo

> > try:

> > self._width,self._height,c=readJPEGInfo(self.fp)

> > except:

> > raise RuntimeError('Imaging Library not available,

> unable to import bitmaps only jpegs')

> > ...


> I agree completely. If PIL cannot be imported, readJPEGInfo should

> raise an exception saying so. If it's otherwise unexpected 'bad JPEG

> data' it should either say so or raise the naked exception.


> I guess we could fix this and write a unit test to verify appropriate

> exceptions occur..


> - Andy

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at lists2.reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20101005/0e09f49e/attachment-0001.html>

More information about the reportlab-users mailing list