Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF, Django, qr-ode, reportlab

I use python-qrcode and reportlab, I want to generate a qrcode and display it without saving it as an image.

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

I have this error:

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'

Thank you

like image 346
Jeremy Avatar asked Sep 15 '25 10:09

Jeremy


2 Answers

I found a solution:

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)
like image 106
Jeremy Avatar answered Sep 16 '25 23:09

Jeremy


Here is a small, working example from what I use. It is similar to Jeremy's solution. But I call the qr_code() in my story-flow:

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)
like image 44
picibucor Avatar answered Sep 17 '25 00:09

picibucor