[reportlab-users] Drawing QRcode

1,021 views
Skip to first unread message

Vaibhav Gajengi

unread,
Aug 26, 2014, 10:39:40 AM8/26/14
to reportlab-users
Hello all,

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

Any help is appreciated 


Best Regards
Vaibhav

Tim Roberts

unread,
Aug 26, 2014, 1:01:42 PM8/26/14
to reportlab-users
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, ti...@probo.com
Providenza & Boekelheide, Inc.

Vaibhav Gajengi

unread,
Aug 27, 2014, 5:28:36 AM8/27/14
to reportlab-users
Hello ,
I tried using that but its not giving me the result.
Can you explain me which info should i give for size of widget ?
I tried with by giving the parameters as below. but i am not getting the exact use of this parameters.

Below is the code part 
qrw = QrCodeWidget(item,barHeight=10*mm,barWidth=10*mm,barBorder=6)
Can you explain the above paramters ? 

Thanks
Vaibhav


_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users


Vaibhav Gajengi

unread,
Aug 27, 2014, 10:45:25 AM8/27/14
to reportlab-users
Hello,

One more Q i want to ask about the rendering the pdf.
i am using the below code snipet for rendering QR Code on the pdf but its not rendering the pdf on given x & y co-ordinates ?
renderPDF.draw(d,p,x,y)
is is there any reason for that ?
Can you explain me the same ??
Any help Appreciated .


Best Regards
Vaibhav

Tim Roberts

unread,
Aug 27, 2014, 1:54:51 PM8/27/14
to reportlab-users
Vaibhav Gajengi wrote:
>
> I tried using that but its not giving me the result.
> Can you explain me which info should i give for size of widget ?
> I tried with by giving the parameters as below. but i am not getting
> the exact use of this parameters.
>
> Below is the code part
> qrw = QrCodeWidget(item,barHeight=10*mm,barWidth=10*mm,barBorder=6)
> Can you explain the above paramters ?

I don't think you should specify ANYTHING in the QrCodeWidget
constructor, because you don't know what the parameters do. You can go
look at the code, if you want, but I don't think it will help.

Just let the widget generate the code in whatever size makes it
comfortable. For the sample I gave, it ends up being just over one inch
square.

Then, resize it to your desired size when you place it into a Drawing.
That's the canonical way to use this widget.

Tim Roberts

unread,
Aug 27, 2014, 2:00:06 PM8/27/14
to reportlab-users
Vaibhav Gajengi wrote:

One more Q i want to ask about the rendering the pdf.
i am using the below code snipet for rendering QR Code on the pdf but its not rendering the pdf on given x & y co-ordinates ?
renderPDF.draw(d,p,x,y)
is is there any reason for that ?
Can you explain me the same ??

Where does it put it?  In the PostScript coordinate system, the (0,0) point is at the bottom left-hand corner.  Y values increase as you go up the page.  That's backwards from what you are used to (unless you worked on OS/2, which is why Windows DIBs are also bottom-up).

Here is the complete code I tested with.  This produces a tiny QR code near the bottom left corner (1 point from each margin):
import os
import sys
from reportlab.pdfgen import canvas
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.barcode.qr import QrCodeWidget
from reportlab.graphics import renderPDF
from reportlab.lib.units import mm

p = canvas.Canvas('qrtest.pdf')

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)

renderPDF.draw(d,p,1,1)
p.showPage()
p.save()
Reply all
Reply to author
Forward
0 new messages