2015-02-17 3 views
0

Как добавить изображение PIL, созданное библиотекой qrcode, в файл PDF. Ниже приведен код и сообщение об ошибке, которое я получаю.python Добавление изображения PIL в файл pdf

> def WriteQRCodeToPDF: 
>  p = canvas.Canvas("document-output.pdf") 
>  pagesize=letter 
>  width, height = letter 
>  image = ImageReader(GenerateQRCode("data")); 
>  p.drawImage(image, inch, height - 2 * inch, 100,100); 
>  p.showPage() 
>  p.save() 
> 
> def GenerateQRCode(qrdata): 
>  qr = qrcode.QRCode(
>   version=1, 
>   error_correction=qrcode.constants.ERROR_CORRECT_L, 
>   box_size=10, 
>   border=4, 
> ) 
>  qr.add_data(qrdata) 
>  qr.make(fit=True) 
>  img = qr.make_image() 
>  return img 

сообщение об ошибке, как указано ниже. Похоже, ImageReader не принимают PIL изображение parameter.What может использовать вместо него

> Traceback (most recent call last): File 
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 657, in open_for_read 
>  return open_for_read_by_name(name,mode) File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 601, in open_for_read_by_name 
>  return open(name,mode) TypeError: invalid file: <qrcode.image.pil.PilImage object at 0xb6a9ccac> 
> 
> During handling of the above exception, another exception occurred: 
> 
> Traceback (most recent call last): File 
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 660, in open_for_read 
>  return getBytesIO(datareader(name) if name.startswith('data:') else urlopen(name).read()) File 
> "/usr/local/lib/python3.4/dist-packages/qrcode/image/pil.py", line 35, 
> in __getattr__ 
>  return getattr(self._img, name) File "/usr/local/lib/python3.4/dist-packages/PIL/Image.py", line 610, in 
> __getattr__ 
>  raise AttributeError(name) AttributeError: startswith 
> 
> During handling of the above exception, another exception occurred: 
> 
> Traceback (most recent call last): File "./qrcodegenerator.py", line 
> 44, in <module> 
>  main() File "./qrcodegenerator.py", line 41, in main 
>  WriteQRCodeToPDF(dataList) File "./qrcodegenerator.py", line 21, in WriteQRCodeToPDF 
>  image = ImageReader(GenerateQRCode("data1")); File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 800, in __init__ 
>  annotateException('\nfileName=%r identity=%s'%(fileName,self.identity())) File 
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 1380, in annotateException 
>  rl_reraise(t,v,b) File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 137, in rl_reraise 
>  raise v File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 764, in __init__ 
>  self.fp = open_for_read(fileName,'b') File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line 
> 662, in open_for_read 
>  raise IOError('Cannot open resource "%s"' % name) OSError: Cannot open resource "<qrcode.image.pil.PilImage object at 0xb6a9ccac>" 
> fileName=<qrcode.image.pil.PilImage object at 0xb6a9ccac> 
> identity=[[email protected]] 
+0

где сообщение об ошибке? –

+0

Я только что добавил – kyy

ответ

1

я использовал QRCode (https://pypi.python.org/pypi/qrcode).

Это фрагмент кода, который работает для меня:

response = HttpResponse(content_type='application/pdf') 
response['Content-Disposition'] = 'attachment; filename="backup_key.pdf"' 
p = canvas.Canvas(response) 
img = qrcode.make(data) 
image = ImageReader(img._img); 
p.drawImage(image, 10, 10); 
Смежные вопросы