如何将qrcode库创建的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图像作为参数。我可以使用它而不是它
> 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=[ImageReader@0xb6a9cc2c]
答案 0 :(得分: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);