Python 使用reportlab和django框架在一个pdf文件中生成多个qr码
使用reportlab,我如何生成一系列二维码并将其放入一个pdf中,然后在用户浏览器上打开它。这是我的尝试。提前谢谢。对于下面的代码,什么都不会发生。我希望得到保存pdf文件的提示Python 使用reportlab和django框架在一个pdf文件中生成多个qr码,python,django,reportlab,Python,Django,Reportlab,使用reportlab,我如何生成一系列二维码并将其放入一个pdf中,然后在用户浏览器上打开它。这是我的尝试。提前谢谢。对于下面的代码,什么都不会发生。我希望得到保存pdf文件的提示 from reportlab.pdfgen import canvas from django.http import HttpResponse from reportlab.graphics.shapes import Drawing from reportlab.graphics.barcode.qr imp
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
你的代码对我有用,但我怀疑这是因为你没有将它封装在视图中 例如,myapp/views.py
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 your views here.
def test_qr(request):
# 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
myproject/url.py
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'myapp.views.test_qr'),
)
打开浏览器,http:127.0.0.1:8000提示我下载在左下角带有二维码的pdf。如果您不知道如何使用Django,我建议您通读一下您能用更具体的方式描述一下您的困难吗?你的代码做什么是对的/错的,你到底在哪里有困难等等。我认为这是一个浏览器兼容性问题。在firefox中可用,但在chrome中不可用(15)。我必须将
HttpResponse(mimetype='application/pdf')
更改为HttpResponse(content_type='application/pdf')
,才能使示例生效