在AppEngine Python上使用Reportlab生成的PDF文档内添加图像文件的正确方法 [英] Proper way to add an image file inside a PDF document generated with Reportlab on AppEngine Python

查看:1712
本文介绍了在AppEngine Python上使用Reportlab生成的PDF文档内添加图像文件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在App Engine Python上使用reportlab生成PDF报告。



但我不知道如何正确添加图片。

>

图像是静态的。



这是我的项目的目录树。




这就是我所做的(在' chipas.py '中)来获取图像:

  im = Image('../ static / logo.png',1 * inch,1 * inch)
story.append(im)
...

Traceback((c))

最近一次调用最后):文件
C:\ Users \Lucas \Dropbox\Desarrollo\Python\windows\AppEngine\google\appengine\ext\webapp_webapp25.py ,
第701行,在调用
handler.get(* groups)文件C:\ Users \ Lucas \Dropbox\Desarrollo\\ workspace \python\chipas-windows\src\chipas.py,
line 1035,in get
doc.build(story)FileC:\Users\Lucas\ Dropbox \Desarrollo\workspace\python\chipas\\windows\src\reportlab\platypus\doctemplate.py,
line 1117,build
BaseDocTemplate.build(self,文件
C:\ Users \Lucas \Dropbox\Desarrollo\workspace\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' py,
line 880,in build
self.handle_flowable(flowables)FileC:\ Users \ Lucas \Dropbox\Desarrollo\workspace\python\chipas-windows\\ \\ src\reportlab\platypus\doctemplate.py,
第763行,在handle_flowable
中if frame.add(f,canv,trySplit = self.allowSplitting):FileC:\ Users\Lucas\Dropbox\Desarrollo\workspace\python\ chipas-windows \src\reportlab\platypus\frames.py,
行159,在_add
w,h = flowable.wrap(aW,h)文件C:\ Users \ Lucas \Dropbox\Desarrollo\workspace\python\chipas\\windows\src\reportlab\platypus\flowables.py,
行408,换行
返回self.drawWidth,self.drawHeight FileC:\ Users \Lucas \Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py,
第402行,位于 getattr
self._setup_inner()文件C:\ Users \ Lucas \Dropbox\\\Desarrollo\workspace\python\chipas -windows \src\reportlab\platypus\flowables.py,
line 368,in _setup_inner
img = self._img FileC:\Users\Lucas\Dropbox\\ \\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\fl owables.py,
第398行,在 getattr中
self._img = ImageReader(self._file)文件C:\ Users \ Lucas \Dropbox\ Desarrollo \workspace\python\chipas\\windows\src\reportlab\lib\utils.py,
行541,在 init
中if _isPILImage (fileName):文件C:\ Users \Lucas\Dropbox\Desarrollo\workspace\python\chipas\\windows\src\reportlab\lib\utils.py,
_line 521,in _isPILImage
return isinstance(im,Image.Image)AttributeError:'NoneType'对象没有属性'Image'INFO 2012-02-29 19:54:37,276
dev_appserver.py: GET
/ pdf?pedido = ahVkZXZ-Y2hpcGFzLWludGhlY2xvdWRyLwsSBlBlZGlkbyIjMjAxMi0wMi0yOSAxOTo1NDoxOHRlc3RAZXhhbXBsZS5jb20M
HTTP / 1.1500 -


在使用reportlab生成的pdf文档中添加图像文件的正确方法?
非常感谢!

解决方案

ReportLab需要PIL处理JPEG以外的图像(PIL在Appengine制作中不可用),但它本身支持JPEG(请参阅下面的注释)。因此,在渲染PDF文件时,您需要为ReportLab提供JPEG图像。使用用户提交的图像,我首先使用Appengine的API在呈现前将图像转换为JPEG。



因此,第一步是将徽标从png更改为jpeg。 p>

其次,Appengine不允许您从静态目录中读取文件。如果你尝试,它会给出'文件不可访问'错误。将.jpg切换到/ static /以外的目录。



因此,无论您用什么方法定义文件的相对路径(gfortune的代码完全适用于此),要渲染图像,只需使用:



f = open(path_to_file,'rb')

story.append(Image(f))

注意:几年前,ReportLab存在一个错误,它仍会尝试访问PIL,即使是JPEG文件也是如此。我与开发人员进行了对话,他们提供了一个绕过PIL处理JPEG文件的构建。我不确定哪个版本是当前版本,请记住这一点。


I'm trying to generate a PDF report using reportlab on App Engine Python.

But I don't know how to attach an image properly.

The image is static.

This is the directory tree of my project.

and this is what I do (inside 'chipas.py') to get the image:

im = Image('../static/logo.png',1*inch, 1*inch)
story.append(im)
...

The stack trace I get:

Traceback (most recent call last): File "C:\Users\Lucas\Dropbox\Desarrollo\Python\windows\AppEngine\google\appengine\ext\webapp_webapp25.py", line 701, in call handler.get(*groups) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\chipas.py", line 1035, in get doc.build(story) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 1117, in build BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 880, in build self.handle_flowable(flowables) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 763, in handle_flowable if frame.add(f, canv, trySplit=self.allowSplitting): File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\frames.py", line 159, in _add w, h = flowable.wrap(aW, h) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 408, in wrap return self.drawWidth, self.drawHeight File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 402, in getattr self._setup_inner() File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 368, in _setup_inner img = self._img File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 398, in getattr self._img = ImageReader(self._file) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\lib\utils.py", line 541, in init if _isPILImage(fileName): File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\lib\utils.py", line 521, in _isPILImage return isinstance(im,Image.Image) AttributeError: 'NoneType' object has no attribute 'Image' INFO 2012-02-29 19:54:37,276 dev_appserver.py:4247] "GET /pdf?pedido=ahVkZXZ-Y2hpcGFzLWludGhlY2xvdWRyLwsSBlBlZGlkbyIjMjAxMi0wMi0yOSAxOTo1NDoxOHRlc3RAZXhhbXBsZS5jb20M HTTP/1.1" 500 -

What's the proper way to add an image file inside a pdf document generated with reportlab? Many thanks in advance!

解决方案

ReportLab requires PIL for images other than JPEG (PIL is not available on Appengine production), but it does support JPEG natively (see note below). So you need to provide ReportLab with JPEG images when rendering PDF files. With user-submitted images, I first use Appengine's API to transform the image to JPEG before rendering.

So step 1 is to change the logo from png to jpeg.

Secondly, Appengine does not allow you to read files from the static directory. It gives a 'File not Accessible' error if you try. Switch the .jpg to a directory other than /static/.

So whatever method you use to define the relative path to the file (gfortune's code works perfectly for this), to render the image, just use:

f = open(path_to_file, 'rb')

story.append( Image(f) )

Note: Several years ago, ReportLab had a bug where it would still try to access PIL, even for JPEG files. I corresponded with the developers, who provided a build that bypassed PIL for JPEG files. I'm not sure which build is the current build, so keep this in mind.

这篇关于在AppEngine Python上使用Reportlab生成的PDF文档内添加图像文件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆