使用reportlab和django框架在一个pdf文件中生成多个qr代码

时间:2021-12-26 09:47:23

Using reportlab, How can I generate a series of qr codes and put them in one pdf and then open it on the user browser. Here is my attempt. Thanks in advance. For this code below, nothing happens. I was expecting to be prompted to save the pdf file.

使用reportlab,我如何生成一系列qr代码并将它们放在一个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

1 个解决方案

#1


4  

Your code worked for me, though I suspect it's because you didn't encapsulate it in a view?

你的代码对我有用,虽然我怀疑是因为你没有将它封装在一个视图中?

For example, myapp/views.py

例如,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/urls.py

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.test_qr'),
)

Opening my browser to say, http:127.0.0.1:8000 prompts me to download the pdf rendered with a QR code at the bottom left corner. If you're not sure how to use Django, I suggest reading through Django Book Online

打开浏览器说,http:127.0.0.1:8000会提示我下载左下角带有QR码的pdf。如果您不确定如何使用Django,我建议您阅读Django Book Online

#1


4  

Your code worked for me, though I suspect it's because you didn't encapsulate it in a view?

你的代码对我有用,虽然我怀疑是因为你没有将它封装在一个视图中?

For example, myapp/views.py

例如,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/urls.py

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.test_qr'),
)

Opening my browser to say, http:127.0.0.1:8000 prompts me to download the pdf rendered with a QR code at the bottom left corner. If you're not sure how to use Django, I suggest reading through Django Book Online

打开浏览器说,http:127.0.0.1:8000会提示我下载左下角带有QR码的pdf。如果您不确定如何使用Django,我建议您阅读Django Book Online