首页 > 解决方案 > Reportlab 上的 Appengine 图像 pdf

问题描述

Python 2.7、谷歌应用引擎、ndb.Model

我将 .jpg 图像存储为

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

这些完美地存储和显示在 html 页面中。但是,我不知道如何使用reportlab 在.pdf 中“打印”这些内容。

我目前的代码是:

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')

我显然在这里遗漏了一些东西,我似乎没有访问实际图像。对我有什么线索吗?

谢谢,大卫

标签: python-2.7app-engine-ndbreportlab

解决方案


感谢您的上述回答,这使我朝着正确的方向前进。以防万一其他人需要帮助,我终于...

from reportlab.lib.utils import ImageReader

# note self.filetext is a ndb.Model with .signblobkey pointing to my signature
self.signature = 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)  
        self.signature = ImageReader(blobattach)

# in the printing routine (I will print a few thousand pages, each with a signature
if self.signature:
    self.p.drawImage(image=self.signature, 
                     x=self.colleft, 
                     y=c_lineprint - (4 * self.lineheight), 
                     width=self.colright - self.colleft, 
                     height=4 * self.lineheight, 
                     mask=None, 
                     preserveAspectRatio=True, 
                     anchor='nw')

推荐阅读