[reportlab-users] Drawing QRcode

Tim Roberts timr at probo.com
Tue Aug 26 13:01:38 EDT 2014


Vaibhav Gajengi wrote:
>
> I want to ask one basic stuff regarding QRcode generation.
> I am using the reportlab 2.7 for drawing the QRcode i want to draw the
> QRcode of 10mm * 10mm.
>
> i want help regarding how to set the width & height of QRcode.
>
> my code snippet is : 
> qrw = QrCodeWidget(itemtodraw,height=10*mm,width=10*mm)
>
> But its not getting correctly drawn of 10*mm by 10*mm

There are a couple of things going on here.

You can't make a QR code arbitrarily small.  The size of the widget
depends on the amount of information you are embedding in the code.  The
QrCodeWidget here ignores the width and height you supply.  It generates
an object using nominal sizing.  You then find out with qrw.getBounds()
how large the nominal code is.

Once you have that, you can scale the code yourself to fit the spot you
have for it when you embed it in a drawing object.  This is why you see
this code in virtually every QR example:
    b = qrw.getBounds()
    w = b[2]-b[0]
    h = b[3]-b[1]

So, this will do what you ask, but the results aren't very interesting
because 10mm is just too small:

    unit = 10*mm
    qrw = QrCodeWidget('hello cruel world!')
    b = qrw.getBounds()
    w = b[2]-b[0]
    h = b[3]-b[1]
    d = Drawing(unit,unit,transform=[unit/w,0,0,unit/h,0,0])
    d.add(qrw)

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20140826/1ad1a506/attachment.html>


More information about the reportlab-users mailing list