python - PDF、Django、二维码、reportlab

标签 python django pdf-generation qr-code reportlab

我使用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/

相关文章:

python - 如何打印 'X' 字符序列中的每 5 个字符?

django - Dockerizing Django 应用程序 : ModuleNotFoundError: No module named 'X"

java - 使用 apache PDF-Box 插入 PDF 附件的缩略图

python - Django Queryset 用于连续查询 first_name 和 last_name 的全名

django - 反向、reverse_lazy、解析和重定向之间有什么区别? [ Django ]

asp.net-mvc - 在帖子上生成涂鸦报告

css - NReco PdfGenerator 字符或字母间距

python - 在具有不同索引的两个数据帧上进行 Pandas 计算

python - 我的 Python 代码出了什么问题?

python - 使用 Python 在 Neo4j 中为其属性创建多个节点