Gnu Barcode

Table of Contents

GNU Barcode 
Trying to understand how to layout printings 

GNU Barcode

GNU Barcode is a tool to convert text strings to printed bars. It supports a variety of standard codes to represent the textual strings and creates postscript output.

Main features of GNU Barcode:

  • Available as both a library and an executable program
  • Supports UPC, EAN, ISBN, CODE39 and other encoding standards
  • Postscript and Encapsulated Postscript output
  • Accepts sizes and positions as inches, centimeters, millimeters
  • Can create tables of barcodes (to print labels on sticker pages)


Alessandro Rubini's free GNU Barcode, a full-featured barcode engine including ISBN support. This is free open source code, and by far the best solution for all UNIX users. Postscript file output (the bars are created on the fly, not from fonts).


yum install barcode.i386 libpaper.i386


Quick Help 

$ barcode -h
barcode: Options:
   -i <arg>     input file (strings to encode), default is stdin
   -o <arg>     output file, default is stdout
   -b <arg>     string to encode (use input file if missing)
   -e <arg>     encoding type (default is best fit for first string)
   -u <arg>     unit ("mm", "in", ...) used to decode -g, -t, -p
   -g <arg>     geometry on the page: [<wid>x<hei>][+<margin>+<margin>]
   -t <arg>     table geometry: <cols>x<lines>[+<margin>+<margin>]
   -m <arg>     internal margin for each item in a table: <xm>[,<ym>]
   -n           "numeric": avoid printing text along with the bars
   -c           no Checksum character, if the chosen encoding allows it
   -E           print one code as eps file (default: multi-page ps)
   -P           create PCL output instead of postscript
   -p <arg>     page size (refer to the man page)

Known encodings are (synonyms appear on the same line):
        "ean", "ean13", "ean-13", "ean8", "ean-8"
        "upc", "upc-a", "upc-e"
        "39", "code39"
        "128c", "code128c"
        "128b", "code128b"
        "128", "code128"
        "i25", "interleaved 2 of 5"
        "cbr", "codabar"
        "pls", "plessey"
        "code93", "93"

Detail Help

The barcode program is a front-end to access some features of the library from the command line. It is able to read user supplied strings from the command line or a data file (standard input by default) and encode all of them.

The Command Line

barcode accepts the following options:

—help or -h
Print a usage summary and exit.
-i filename
Identify a file where strings to be encoded are read from. If missing (and if -b is not used) it defaults to standard input. Each data line of the input file will be used to create one barcode output.
-o filename
Output file. It defaults to standard output.
-b string
Specify a single "barcode" string to be encoded. The option can be used multiple times in order to encode multiple strings (this will result in multi-page postscript output or a table of barcodes if -t is specified). The strings must match the encoding chosen; if it doesn't match the program will print a warning to stderr and generate "blank" output (although not zero-length). Please note that a string including spaces or other special characters must be properly quoted.
-e encoding
encoding is the name of the chosen encoding format being used. It defaults to the value of the environment variable BARCODE_ENCODING or to auto detection if the environment is also unset.
-g geometry
The geometry argument is of the form "[<width> x <height>] [` <xmargin> ` <ymargin>]" (with no intervening spaces). Unspecified margin values will result in no margin; unspecified size results in default size. The specified values represent print points by default, and can be inches, millimeters or other units according to the -u option or the BARCODE_UNIT environment variable. The argument is used to place the printout code on the page. Note that an additional white margin of 10 points is added to the printout. If the option is unspecified, BARCODE_GEOMETRY is looked up in the environment, if missing a default size and no margin (but the default 10 points) are used.
-t table-geometry
Used to print several barcodes to a single page, this option is meant to be used to print stickers. The argument is of the form "<columns> x <lines> [` <leftmargin> ` <bottommargin> [- <rightmargin> [- <topmargin>]]]" (with no intervening spaces); if missing, the top and right margin will default to be the same as the bottom and left margin. The margins are specified in print points or in the chosen unit (see -u below). If the option is not specified, BARCODE_TABLE is looked up in the environment, otherwise no table is printed and each barcode will get its own page. The size (but not the position) of a barcode item within a table can also be selected using -g (see "geometry" above), without struggling with external and internal margins. I still think management of geometries in a table is suboptimal, but I can't make it better without introducing incompatibilities.
-m margin(s)
Specifies an internal margin for each sticker in the table. The argument is of the form "<xmargin>,<ymargin>" and the margin is applied symmetrically to the sticker. If unspecified, the environment variable BARCODE_MARGIN is used or a default internal margin of 10 points is used.
"Numeric" output: don't print the ASCII form of the code, only the bars.
No checksum character (for encodings that allow it, like code 39, other codes, like UPC or EAN, ignore this option).
Encapsulated postscript (default is normal postscript). When the output is generated as EPS only one barcode is encoded.
PCL output. Please note that the Y direction goes from top to bottom for PCL, and the origin for an image is the top-left corner instead of the bottom-left
-p pagesize
Specify a non-default page size. The page size can be specified in millimeters, inches or plain numbers (for example: "210x297mm", "8.5x11in", "595x842"). A page specification as numbers will be interpreted according to the current unit specification (see -u below). If libpaper is available, you can also specify the page size with its name, like "A3" or "letter" (libpaper is a standard component of Debian GNU/Linux, but may be missing elsewhere). The default page size is your system-wide default if libpaper is there, A4 otherwise.
-u unit
Choose the unit used in size specifications. Accepted values are "mm", "cm", "in" and "pt". By default, the program will check BARCODE_UNIT in the environment, and assume points otherwise (this behaviour is compatible with 0.92 and previous versions. If -u appears more than once, each instance will modified the behaviour for the arguments at its right, as the command line is processes left to right. The program internally works with points, and any size is approximated to the nearest multiple of one point. The -u option affect -g (geometry), -t (table) and -p (page size).


Grand & Toy # 99773,

Label width: 44 mm
Label height: 12 mm
Horizontal gap between labels 8 mm
Vertical gap between labels: 1 mm
Top margin: 13 mm
Left margin: 8 mm

Should be:

yes 12345 | head -72 | barcode -p 8.5x11in -umm -t 4x18+8+13 -g 44x12+8+1 -e CODE39 >

But in fact:

yes 12345 | head -84 | barcode -p 8.5x11in -umm -t 4x21+0+0-0-12.8 -g 36x12+7+0 -m 5,0 -e CODE39 >; date '+%H:%M:%S'

Trying History 

$ barcode -umm -p letter -t 4x18+8+13 -g 44x12+8+1 -e CODE39  -b 1234567890 -b 1234567890 -b 1234567890 -b 1234567890 -b 1234567890 -b 1234567890 >
barcode: wrong page size specification "letter"
barcode: try "barcode --help"

-p 8.5x11in works.

$ yes 12345 | head -72 | barcode -umm -p 215x279mm -t 4x18+0+12-0-12 -g 32x8.5+7+0 -m 0,0 -e CODE39 >; lpr; date '+%H:%M:%S'
# add l/r margin = 8
$ yes 12345 | head -72 | barcode -umm -p 215x279mm -t 4x18+0+12-0-12 -g 32x8.5+7+0 -m 8,0 -e CODE39 >; lpr; date '+%H:%M:%S'
$ yes 12345 | head -80 | barcode -p 8.5x11in -umm -t 4x20+8+13 -g 44x12+8+0 -m 5,0 -e CODE39 >; date '+%H:%M:%S'
$ yes 12345 | head -84 | barcode -p 8.5x11in -umm -t 4x21+8+0-8-12 -g 44x12+8+0 -m 5,0 -e CODE39 >; date '+%H:%M:%S'
$ yes 12345 | head -84 | barcode -p 8.5x11in -umm -t 4x21+0+0-0-12.8 -g 36x12+7+0 -m 5,0 -e CODE39 >; date '+%H:%M:%S'

Trying to understand how to layout printings 

I have to say that I was having a hard time trying to figure how to layout printings with the front-end barcode program. I thought it shouldn't be too hard, but over 50 test pages printed, I am still unable to understand how it works, and still very confused. For example, the following is what I found.

  • If all else are the same, -t 4x18+0+12-0-12, -t 4x18+8+12-8-12, & -t 4x18+28+12-0-12 will print the same output.
  • If all else are the same, -t 4x18+0+0-0-0, -t 4x18+0+20-0-2, -t 4x18+0+18-0-2 will print the same output.
  • Contradicting to what user manual says, to increase the gap between the barcodes labels, I need to decrease the "+<xmargin>" value in the -g parameter instead of increasing it. E.g., from -g 34x10+19+1, the gap will become smaller and smaller if I use -g 34x9.5+21+0 or -g 34x8.5+24+0.
  • the -m does not works as expected at all.
  • no matter what bottommargin I used, the print out would be the same (Check the ending 3 commands).
  • Contradicting to what user manual indicates, changing from '-g 44x12+8+0.8' to '-g 44x10+8+0' will not make labels take up less space vertically, but the total vertical space increases instead.

    yes 12345 | head -72 | barcode -p 8.5x11in -umm -t 4x18+8+13 -g 44x10+8+0 -m 5,0 -e CODE39 >
  • changing bottommargin has no effect at all: '-t 4x18+8+13-8-13', '-t 4x20+8+60-8-0' & '-t 4x20+8+80-8-0' produce the same bottom margin, and it prints off the page.
  • No way to precisely define top margin at all: '-t 4x21+8+0-8-12', '-t 4x21+8+0-8-12.9', & '-t 4x21+8+0-8-13' is producing the same top margin, and so will '-t 4x21+8+0-8-14' & '-t 4x21+8+0-8-15' be.
  • Not being able to precisely define top margin makes the program almost useless. All my labels are off the grid. In order circumvent the problem and limitation, I tried to decrease the page length, but that produces big jumps as well.
  • If all else are the same, changing page-size unit without changing the actual page size still produces different print out. E.g., the following 2 commands print different print outs:

    yes 12345 | head -84 | barcode -p 8.5x11in -umm -t 4x21+0+0-0-12.8 -g 36x12+7+0 -m 5,0 -e CODE39 >
    yes 12345 | head -84 | barcode -umm -p 215x279 -t 4x21+0+0-0-12.8 -g 36x12+7+0 -m 5,0 -e CODE39 >

Over 10 hours past (19 hours on wall clock) and over 80 test pages printed, I gave up trying. All my labels are still off the grid, but there is nothing further I can twist. This is the worst program that I ever used. Hope my end user can settle down with such "solution". Else, I'll look for something else.

documented on: 2007.11.01