Search code examples
python-2.7app-engine-ndbreportlab

Appengine images on Reportlab pdf


Python 2.7, Google App Engine, ndb.Model

I have .jpg images stored as

signblobkey      = ndb.BlobKeyProperty(verbose_name='Signature')

These store and display in html pages perfectly. However, I can't figure out how to 'print' these in a .pdf with reportlab.

My current code is:

blobattach = ''
blobname   = ''
blobmime   = 'None'
if self.filetext.signblobkey != None:
    blob_info  = blobstore.BlobInfo.get(self.filetext.signblobkey)
blobmime   = blob_info.content_type[:5]
blobname   = blob_info.filename
if blobmime == 'image':
    blobattach = get_serving_url(self.filetext.signblobkey)          

canvas.ImageReader(StringIO.StringIO(self.filetext.signblobkey))
self.p.drawImage(image=blobattach,
                 x=self.colleft, 
                 y=c_lineprint - (4 * self.lineheight), 
                 width=self.colright - self.colleft, 
                 height=4 * self.lineheight, 
                 mask=None, 
                 preserveAspectRatio=True, 
                 anchor='nw')

I am obviously missing something here, I don't seem to be accessing the actual image. Any clues for me?

Thanks, David


Solution

  • Well, what you're passing as the image to self.p.drawImage is just blobattach, which, at best, is just a URL. You probably need to pass it an image.

    You could try to pass that URL to ImageReader (which apparently knows how to make use of a URL) and use the result as the image. What you're passing right now to ImageReader is a not a URL. I'm not quite sure what it is - I doubt StringIO (or ImageReader for that matter) knows what to do with a blob key (your self.filetext.signblobkey).

    Or you could try to read the image from the blobstore directly, from your self.filetext.signblobkey. Something along these lines (you need to use the blobstore API to properly interpret a blob key):

    with blobstore.BlobReader(self.filetext.signblobkey) as fd:
         blob_content = fd.read()
    

    Not sure how exactly how it'd work, tho, I didn't play with images (yet).