【问题标题】:PDF, Django, qr-ode, reportlabPDF、Django、二维码、reportlab
【发布时间】:2013-07-21 08:44:44
【问题描述】:

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

谢谢

【问题讨论】:

  • 你能给出错误的完整堆栈跟踪吗?

标签: python django pdf-generation qr-code reportlab


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • hmm ...所以第一个块是解决方案?
    【解决方案3】:

    这是我使用的一个小而有效的示例。 它类似于 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)
    

    【讨论】:

      【解决方案4】:

      我找到了解决办法:

      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 从何而来?
      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2015-10-11
      相关资源
      最近更新 更多