Reportlab 上的 Appengine 图片 pdf

Appengine images on Reportlab pdf

Python 2.7,Google 应用引擎,ndb.Model

我将 .jpg 图像存储为

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

这些在 html 页面中完美存储和显示。但是,我不知道如何使用 reportlab 在 .pdf 中 'print' 这些。

我当前的代码是:

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

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

谢谢, 大卫

好吧,您作为 image 传递给 self.p.drawImage 的只是 blobattach,充其量只是 URL。您可能需要将图像传递给它。

您可以尝试将 URL 传递给 ImageReader(它显然知道如何使用 URL)并将结果用作图像。您现在传递给 ImageReader 的不是 URL。我不太确定它是什么 - 我怀疑 StringIO(或 ImageReader 就此而言)知道如何处理 blob 键(您的 self.filetext.signblobkey)。

或者您可以尝试直接从您的 self.filetext.signblobkey 中的 blobstore 中读取图像。沿着这些线的东西(你需要使用 blobstore API 来正确解释 blob 键):

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

不确定它到底是如何工作的,不过,我还没有玩过图像。

感谢上面的回答让我找到了正确的方向。为了以防万一其他人需要帮助,我终于...

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