0

我正在尝试在谷歌应用引擎上使用 xhtml2pdf 生成 PDF。我已经完成了大部分工作,包括将图像嵌入到模板中。但是,这只适用于可公开访问的图像(xhtml2pdf 蚀刻图像以嵌入它们。)我需要能够将一些图像保密。因此,我希望能够使用 data uri 方案将一些图像直接嵌入到 html 中。可悲的是,当我这样做时出现错误:

有问题的html标签是:

<img src='data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACPAOYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4%2BTl5ufo6erx8vP09fb3%2BPn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3%2BPn6/9oADAMBAAIRAxEAPwD3%2BiiigAooooAKKKKACiiigAooooAKKKKAC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KWigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP//Z' width='230' height='143' >

我得到的错误是:

Traceback (most recent call last):
  File "/Users/hamish/dev/kp3/src/libs/xhtml2pdf/tags.py", line 332, in start
    height=None)
  File "/Users/hamish/dev/kp3/src/libs/xhtml2pdf/xhtml2pdf_reportlab.py", line 476, in __init__
    img = self.getImage()
  File "/Users/hamish/dev/kp3/src/libs/xhtml2pdf/xhtml2pdf_reportlab.py", line 497, in getImage
    img = PmlImageReader(StringIO.StringIO(self._imgdata))
  File "/Users/hamish/dev/kp3/src/libs/xhtml2pdf/xhtml2pdf_reportlab.py", line 343, in __init__
    self._image = self._read_image(self.fp)
  File "/Users/hamish/dev/kp3/src/libs/xhtml2pdf/xhtml2pdf_reportlab.py", line 373, in _read_image
    return PILImage.open(fp)
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file fileName=<StringIO.StringIO instance at 0x10503ca70>
4

1 回答 1

0

您可以查看 /Library/Python/2.7/site-packages/PIL/Image.py 内部,或者甚至可以稍微破解一下,看看到底是什么原因让那里不高兴:

文件“/Library/Python/2.7/site-packages/PIL/Image.py”,第 1980 行,打开引发 IOError(“无法识别图像文件”)

假设这是私有文件和公共文件的访问权限问题,请检查两种访问类型之间的区别。

如果您的应用程序静态地为这两种文件提供服务,并且您允许通过app.yaml 文件中的application_readable: true标志进行公共访问,则您需要对私有文件执行相同的操作,并可能使用login: requiredlogin: admin保护它们。

如果私有保护来自您的应用程序,请检查您是如何做到这一点的,如果可能的话,可能会相应地调整 xhtml2pdf 访问。

于 2015-04-17T14:40:43.557 回答