我在使用Vagrant的VM(2.2.3)使用ReportLab abd PIL(在Flask项目中)时遇到问题
使用 readJPEGInfo()函数定位错误。那里是PNG文件( -rwxrwxr-- 1 www-data www-data 21K juin 22 ):
site_media/img/84/5_1_pied_de_page.png: PNG image data, 1066 x 47, 8-bit/color RGBA, non-interlaced
错误消息:
(unpack requires a string argument of length 1 Imaging Library not available, unable to import bitmaps only jpegs
fileName='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png' identity=[ImageReader@0x7fdd75f2a110
filename='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png']
fileName='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png' identity=[ImageReader@0x7fdd75f2a110
filename='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png'] --> <class 'struct.error'> => Traceback (most recent call last): File "/var/www/my_project/my_project/flask_my_project/views.py", line 206, in zonesmeteo createDisplay(t, config, tabAlerts, demain, message)
File "/var/www/my_project/my_project/flask_my_project/utils.py", line 29, in createDisplay tr.generateFiles()
File "/var/www/my_project/my_project/cron_dept_treatments/84.py", line 64, in generateFiles self.dicoFolders)
File "/var/www/my_project/my_project/cron_dept_treatments/84.py", line 258, in __init__ table5.drawOn(self.c,15,12)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 111, in drawOn self._drawOn(canvas)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 92, in _drawOn self.draw()#this is the bit you overload
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/tables.py", line 1425, in draw self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/tables.py", line 1558, in _drawCell v.drawOn(self.canv,x,y)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 111, in drawOn self._drawOn(canvas)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 92, in _drawOn self.draw()#this is the bit you overload
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 511, in draw self.canv.drawImage( self._img or self.filename,
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/platypus/flowables.py", line 481, in __getattr__ self._img = ImageReader(self._file)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 810, in __init__ annotateException('\nfileName=%r identity=%s'%(fileName,self.identity()))
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 1390, in annotateException rl_reraise(t,v,b)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 804, in __init__ annotateException('\nImaging Library not available, unable to import bitmaps only jpegs\nfileName=%r identity=%s'%(fileName,self.identity()))
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 1390, in annotateException rl_reraise(t,v,b)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 802, in __init__ self._width,self._height,c=readJPEGInfo(self.fp)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/pdfbase/pdfutils.py", line 243, in readJPEGInfo x = struct.unpack('B', image.read(1)) error: unpack requires a string argument of length 1 Imaging Library not available, unable to import bitmaps only jpegs fileName='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png' identity=[ImageReader@0x7fdd75f2a110
filename='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png'] fileName='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png' identity=[ImageReader@0x7fdd75f2a110
filename='/var/www/my_project/my_project/site_media/img/20/5_1_pied_de_page.png'] )
pip冻结的部分(python 2.7):
Pillow==2.6.1
reportlab==3.4.0
用户/组:
$ cat /etc/group | grep data
www-data:x:33:vagrant,apache,nginx,httpd,www-data,www-user
所以,我做了一个简单的python测试文件:
import reportlab, PIL
from reportlab.platypus import SimpleDocTemplate
from reportlab.pdfgen import canvas
from reportlab.platypus import Frame, Image
png_file = "/var/www/test/5_1_pied_de_page.png"
img=canvas.Canvas(png_file)
my_doc = SimpleDocTemplate('myfile.pdf')
parts=[]
parts.append(Image(png_file))
my_doc.build(parts)
相同错误:
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/lib/utils.py", line 802, in __init__
self._width,self._height,c=readJPEGInfo(self.fp)
File "/home/vagrant/.virtualenvs/my_project/lib/python2.7/site-packages/reportlab/pdfbase/pdfutils.py", line 243, in readJPEGInfo
x = struct.unpack('B', image.read(1))
struct.error: unpack requires a string argument of length 1
Imaging Library not available, unable to import bitmaps only jpegs
fileName='/var/www/my_project/my_project/site_media/img/84/5_1_pied_de_page.png' identity=[ImageReader@0x7fc9223f3e50 filename='/var/www/my_project/my_project/site_media/img/84/5_1_pied_de_page.png']
fileName='/var/www/my_project/my_project/site_media/img/84/5_1_pied_de_page.png' identity=[ImageReader@0x7fc9223f3e50 filename='/var/www/my_project/my_project/site_media/img/84/5_1_pied_de_page.png']
(my_project)
而且,在我的测试文件中,我已经将JPG转换为PNG:没有错误,但pdf文件为空白:)
谢谢。
F。
$ uname -a
Linux vm 4.4.0-131-generic #157-Ubuntu SMP Thu Jul 12 15:51:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
答案 0 :(得分:0)
我相信您需要将其放入Reportlab可流动对象中。即使在这种情况下我不直接使用PIL,我也可以分享如何使其工作(这里有框架,但也可以不使用)。它适用于大多数图片格式,包括png,请注意,我尚未测试以下代码。
from reportlab.lib.units import cm
from reportlab.platypus import Image, Frame, PageTemplate, BaseDocTemplate
png_file = "/var/www/test/5_1_pied_de_page.png"
picture_frame = Frame(
x1 =10 * cm, # From left
y1 =26 * cm, # From bottom
height =9 * cm,
width =9 * cm)
frontpage = PageTemplate(id='FrontPage', onPage='', frames=[picture_frame])
picture = Image(png_file, 9 * cm, 6 * cm, hAlign='RIGHT')
my_doc = BaseDocTemplate('myfile.pdf', pagesize=A4)
my_doc.addPageTemplates(frontpage)
parts = []
parts.append(picture)
my_doc.build(parts)