All Barcode generators on npm i found included the redering code. Since i was working on my code 128 font and had some example encoder anyways, i decided to write a module which follows the „seperation of concerns“ paradigma. So my new module is a code-128 encoder without the rendering. You can easily plug your own renderer.
This has many advadages. Most use cases of barcodes involve Databases and specialized layouts anyways, and with this approach its easier to integrate into existing workflows.
The encoder i’ve written is installable vie npm or bower, and is usable from the command line, as a node.js package, or directly in the browser.
if you want the cli, install globaly width
npm install -g code-128-encoder
if you want to use the module in your own modules
npm install --save code-128-encoder
and if you like to use it client side
bower install --save code-128-encoder
On github you will find example renderers using fonts, canvas, css/divs, ascii art, svg or latex.
here is a little example: just install the cli globaly (see above), and create a bash script like this:
#!/bin/bash
clear
enc=$(encode128 -o bars $1)
enc2=${enc//1/█}
enc3=${enc2//0/\x20}
echo
echo
for i in 'seq 1 20'
do
echo -e ' '$enc3
done
echo
size0=${#1}
size1=$((${#enc}+20))
margin=$(($size1 - $size0))
margin=$(($margin / 2))
for i in 'seq 1 $margin'
do
echo -ne " "
done
echo $1
echo
echo
which will output the barcode directly to your console 😉
