Posted on

Generating Barcode Numbers and EAN barcode artwork with GNU barcode

Just a quick little post to remind myself how I did this in a few years time! Given a range of barcode numbers, generate the EAN number and artwork for them on the command-line using GNU barcode.

So we have a range like: 934955400000 - 934955400999 and we need to add the check digit and then generate the artwork.

Firstly we need an actual input file representing all of the barcodes in the range. For that we can just use a bash loop (or several).

# this could be a single loop that calculates the padding, but I went for keeping it simple.
for i in {0..9} ; do echo 93495540000$i; done > input.txt
for i in {10..99} ; do echo 9349554000$i; done >> input.txt
for i in {100..999} ; do echo 934955400$i; done >> input.txt

So now we have an input file with all of our numbers in it like this:

$ head input.txt 
934955400000
934955400001
934955400002
934955400003
934955400004
...
$ tail input.txt
...
934955400996
934955400997
934955400998
934955400999

Now we can send that data to the GNU barcode tool to work it’s magic. In its most basic form the command is like this for EAN barcodes. Other types of barcodes can also be generated, check the man page for more info on supported types.

barcode -i input.txt -t EAN -o output.ps

This will generate a post script file called output.ps that contains the artwork and EAN number including check digit – one per page.

But we’d like it if the barcodes were on the pages more efficiently, so we can specify a table layout and page size below.

barcode -p 8.5x11in -t 4x6 -i input.txt -t EAN -o output.ps

I tried multiple different page sizes and table layouts and honestly I have no idea what the parameters really do, they seemed divorced from what you’d expect to see in the actual output file, so it’s a bit of trial and error.

Lastly you can convert the .ps file to a more useful .pdf like so:

ps2pdf output.ps

And then you’re all done. You should get a nice PDF file full of barcodes.