使用reportlab和Django fram在一个pdf文件中生成多个qr代码

2024-05-15 21:08:02 发布

您现在位置:Python中文网/ 问答频道 /正文

使用reportlab,我如何生成一系列二维码并将它们放在一个pdf文件中,然后在用户浏览器上打开它。这是我的尝试。提前谢谢。对于下面的代码,什么都不会发生。我希望有人提示我保存pdf文件。

from reportlab.pdfgen import canvas
from django.http import HttpResponse
from reportlab.graphics.shapes import Drawing 
from reportlab.graphics.barcode.qr import QrCodeWidget 
from reportlab.graphics import renderPDF
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

p = canvas.Canvas(response)

qrw = QrCodeWidget('Helo World!') 
b = qrw.getBounds()

w=b[2]-b[0] 
h=b[3]-b[1] 

d = Drawing(45,45,transform=[45./w,0,0,45./h,0,0]) 
d.add(qrw)

renderPDF.draw(d, p, 1, 1)

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

Tags: 文件thefromimportpdfresponsecanvas二维码