PDF, Django, qr-ode, reportlab

4
我使用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'

谢谢你


你能提供错误的完整堆栈跟踪吗? - user2357112
4个回答

1
这是一个小型的、可运行的例子,与Jeremy的解决方案类似。但我在我的故事流中调用qr_code()函数:
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.barcode import qr
from reportlab.lib.pagesizes import cm,A4

def qr_code(table):
    # generate and rescale QR
    qr_code = qr.QrCodeWidget(table)
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    drawing = Drawing(
        3.5 * cm, 3.5 * cm, transform=[3.5 * cm / width, 0, 0, 3.5 * cm / height, 0, 0])
    drawing.add(qr_code)

    return drawing


doc = BaseDocTemplate('temp_qr.pdf', pagesize=A4)
full_frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 0.5 * doc.bottomMargin, id='full_frame')
full_page = PageTemplate(id='full_page', frames=[full_frame])
doc.addPageTemplates([full_page])
datatable = [['a', 'b', 'c', 'd', 'e', 'f', 'g'],['g', 'f', 'e', 'd', 'c', 'b', 'a']]
story = []
story.append(qr_code(datatable))
doc.build(story)

1
我找到了一个解决方案:
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)

renderPDF来自哪里? - Olivier Pons

1

我使用的qrcode python代码单独运行正常,但在函数中却无法运行。这是本论坛唯一可运行的代码。

import qrcode
from PIL import Image
img = qrcode.make("ada")
print img
img.save('asda.bmp')

它不是那样工作的

  def ann(self):
       import qrcode
       from PIL import Image
       img = qrcode.make("ada")
       print img
       img.save('asda.bmp')

嗯...那第一个块就是解决方案了吗? - kleopatra

0

我以Jeremy和Picibucor的代码作为参考。

from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.barcode import qr
from reportlab.lib.pagesizes import A4
from reportlab.graphics import renderPDF

def docPdf(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="file.pdf"'
    p = canvas.Canvas(response)
    p.drawString(60, 720, "Bienvenido")
    p.drawString(60, 700, "Señor Vende Humo")

    fecha = str(date.today())

    qr_code = qr.QrCodeWidget("Constancia de Alumno Regular emitida el dia: "+fecha)
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    c = Drawing(45, 45, transform=[200./width, 0, 0, 200./height, 0, 0])
    c.add(qr_code)
    renderPDF.draw(c, p, 320, 600)
    p.showPage()
    p.save()
    return response

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接