PDF,Django,qr-ode,reportlab

时间:2013-07-22 14:46:41

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

我使用python-qrcode和reportlab,我想生成一个qrcode并显示它而不将其保存为图像。

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'

谢谢

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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')

答案 2 :(得分: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)

答案 3 :(得分: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