[reportlab-users] Bug when drawing human readable barcodes

Stéphane Travostino stephane.travostino at gmail.com
Thu Nov 20 09:29:43 EST 2008


Hello,
I spotted a bug in the barcode addon, and specifically when trying to draw a
human readable barcode.
Consider this simple example:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.graphics.barcode import code39

TEXT = "TEST01L4"

c=canvas.Canvas("barcode.pdf",pagesize=A4)
barcode=code39.Standard39(TEXT, barWidth=0.423*mm, barHeight=10*mm,
ratio=2.75, humanReadable=True)
barcode.drawOn(c,50*mm,50*mm)
c.showPage()
c.save()

When run on Ubuntu (python-reportlab 2.1 from intrepid repos and 2.2
imported from debian sid), I get this backtrace:
...
File "/usr/lib/python2.5/site-packages/reportlab/pdfbase/pdfmetrics.py",
line 399, in _py_stringWidth
if not isinstance(text,unicode): text = text.decode(encoding)
AttributeError: 'float' object has no attribute 'decode'


It seems that somewhere the "text" variable is substituted with a float
value. Here is the patch for this issue (diffed from SVN
https://svn.reportlab.com/svn/public/reportlab/trunk/src/reportlab/graphics/barcode/common.py
):
$ diff -ruN common.py
/usr/lib/python2.5/site-packages/reportlab/graphics/barcode/common.py
--- common.py 2007-04-13 14:32:39.000000000 +0200
+++ /usr/lib/python2.5/site-packages/reportlab/graphics/barcode/common.py
2008-11-20 15:19:38.000000000 +0100
@@ -173,7 +173,7 @@
if anchor=='middle': func = 'drawCentredString'
elif anchor=='end': func = 'drawRightString'
else: func = 'drawString'
- getattr(canv,func)(text,x,y)
+ getattr(canv,func)(x,y,text)
canv.restoreState()

class MultiWidthBarcode(Barcode):

Stéphane.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20081120/6d6a59e8/attachment.html>


More information about the reportlab-users mailing list