Reportlab and Barcodes

J

Josh

Hi All,

I need someone to explain to me how to output a Code39 barcode to a
Reportlab PDF. As far as I can tell, there is no means to do this with
the Canvas object, and the demo that is included with the libraries is
using the platypus Frame to place the barcode on the form. I do not
wish to use this method. I'm guessing that it needs to be placed on the
form as some type of graphic. It's obviously not going to work with the
drawText method. Any ideas?

Thanks

Josh
 
N

Neil Benn

Josh said:
Hi All,

I need someone to explain to me how to output a Code39 barcode to a
Reportlab PDF. As far as I can tell, there is no means to do this with
the Canvas object, and the demo that is included with the libraries is
using the platypus Frame to place the barcode on the form. I do not
wish to use this method. I'm guessing that it needs to be placed on the
form as some type of graphic. It's obviously not going to work with the
drawText method. Any ideas?

Thanks

Josh
Hello,

Code39 isn't that complicated a barcode symbology to work
with. What you can do is to write some code which can encode a Code39
barcode from a string into a simple graphic. I used to have the
official spec for the barcode but that was in an old job. You can get
the official spec from AIM - you'll have to pay for it but it's not
much. Although beware about the Narrow:Width ratio if you're gonna try
and scan the barcode after printing it out - try and get hold of a
crappy scanner (I got stung by using a good scanner before in the past).

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 46
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : (e-mail address removed)
Cenix Website : http://www.cenix-bioscience.com
 
J

Josh

Benji,

I have used those very same fonts before and they work great, but I'm
trying to get away with using straight Python to accomplish this,
especially since the program will most likely be used on both Linux and
Windows.

Josh
 
J

Josh

One of the users on the Reportlabs mailing list was kinda enough to
offer me the solution.. A simple call to the drawOn function, e.g.:

bc = code39.Standard39("123",xdim = .015*inch)
x = 6*inch
y = -5*inch
bc.drawOn(canvas,x,y)
 
D

Damjan

One of the users on the Reportlabs mailing list was kinda enough to
offer me the solution.. A simple call to the drawOn function, e.g.:

bc = code39.Standard39("123",xdim = .015*inch)

whats code39 in this example?
 
J

Jaime Wyant

That looks cleaner than mine. I had to do this ->

# Register the barcode true-type-font
# Don't want to push the font out to everyone in the office...
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont( TTFont( 'barcode',
r'c:\inetpub\wwwroot\barcode\fre3of9x.ttf'))

# 'c' is the canvas
c.setFont( "barcode", 40 )
c.drawString( x * inch, y * inch, text )

jw
 
J

Josh

Damjan,

Code39 here refers to part of the Barcode Extensions available to
Reportlabs. It can be imported as such

from reportlab.extensions.barcode import code39

Josh
 

czl

Joined
Dec 24, 2008
Messages
1
Reaction score
0
How to get human readable barcode

Hello All,
how to get human readable barcode?
I have found example of code: two.pairlist.net/pipermail/reportlab-users/2008-November/007685.html
Code:
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()
After runing I have got:
Traceback (most recent call last):
File "E:\reportlab-barcode\1.py", line 11, in <module>
barcode.drawOn(c,50*mm,50*mm)
File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py", line 102
, in drawOn
self._drawOn(canvas)
File "C:\Python25\lib\site-packages\reportlab\platypus\flowables.py", line 86,
in _drawOn
self.draw()#this is the bit you overload
File "C:\Python25\lib\site-packages\reportlab\graphics\barcode\common.py", lin
e 146, in draw
self.drawHumanReadable()
File "C:\Python25\lib\site-packages\reportlab\graphics\barcode\common.py", lin
e 164, in drawHumanReadable
self.annotate(x+width/2.,-y,s,fontName,fontSize)
File "C:\Python25\lib\site-packages\reportlab\graphics\barcode\common.py", lin
e 176, in annotate
getattr(canv,func)(text,x,y)
File "C:\Python25\lib\site-packages\reportlab\pdfgen\canvas.py", line 1217, in
drawCentredString
width = self.stringWidth(text, self._fontname, self._fontsize)
File "C:\Python25\lib\site-packages\reportlab\pdfgen\canvas.py", line 1320, in
stringWidth
(fontSize,self._fontsize)[fontSize is None])
File "C:\work\reportlab-2.2\src\rl_addons\rl_accel\_rl_accel.c", line 1104, in
stringWidthU
File "C:\work\reportlab-2.2\src\rl_addons\rl_accel\_rl_accel.c", line 1147, in
_instanceStringWidthU
AttributeError: decode
How to execute abovesaid code?
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top