gpt4 book ai didi

python - PDF、Django、二维码、reportlab

转载 作者:行者123 更新时间:2023-11-28 16:42:17 31 4
gpt4 key购买 nike

我使用python-qrcode和reportlab,我想生成一个二维码并显示它而不是将它保存为图像。

def member_card(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="carte-membre.pdf"'

p = canvas.Canvas(response)

customer = request.user.customer
p.drawImage('./static/skin/img/carte-membre/carte-membre.jpg', 0, 530)
p.drawString(15, 720, customer.first_name + " " + customer.last_name)
p.drawString(15, 700, "Identifiant: " + customer.zipcode[:2] + " " + unicode(customer.id))
qr = qrcode.make(customer.first_name + "+" + customer.last_name + "+" + customer.zipcode[:2] + "+" + unicode(customer.id))
p.drawImage(qr , 170, 690, 70, 60)

p.showPage()
p.save()
return response

我有这个错误:

Traceback (most recent call last):

File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, i
n get_response
response = callback(request, *callback_args, **callback_kwargs)

File "c:\Users\Jeremy\Desktop\Izicap\django\izicap\customer\views.py", line 45
, in member_card
p.drawImage(qr , 170, 690, 70, 60)

File "c:\Python27\lib\site-packages\reportlab\pdfgen\canvas.py", line 926, in
drawImage
imgObj = pdfdoc.PDFImageXObject(name, image, mask=mask)

File "c:\Python27\lib\site-packages\reportlab\pdfbase\pdfdoc.py", line 2123, i
n __init__
ext = string.lower(os.path.splitext(source)[1])

File "c:\Python27\lib\ntpath.py", line 190, in splitext
return genericpath._splitext(p, sep, altsep, extsep)


File "c:\Python27\lib\genericpath.py", line 91, in _splitext
sepIndex = p.rfind(sep)


AttributeError: 'bool' object has no attribute 'rfind'

谢谢

最佳答案

我找到了一个解决方案:

qr_code = qr.QrCodeWidget(customer.first_name + "+" + customer.last_name + "+" + customer.zipcode[:2] + "+" + unicode(customer.id))

bounds = qr_code.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
c = Drawing(45, 45, transform=[60./width, 0, 0, 60./height, 0, 0])
c.add(qr_code)
renderPDF.draw(c, p, 170, 690)

关于python - PDF、Django、二维码、reportlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17790564/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com