Skip to content

Instantly share code, notes, and snippets.

@alexander-bauer
Last active August 29, 2015 14:01
Show Gist options
  • Save alexander-bauer/a1a64fa735ec67a9e3bb to your computer and use it in GitHub Desktop.
Save alexander-bauer/a1a64fa735ec67a9e3bb to your computer and use it in GitHub Desktop.
Create printable LTO3 barcode labels with specific parameters
#!/bin/sh
#
## gen-barcode.sh
#
# This script can be used to generate LTO-3 compatible barcodes using
# GNU Barcode. It is designed for a particular label format, so that
# they can be printed on in regular printers, but can be adapted to
# other purposes.
#
# The script takes three label arguments, none of which may exceed 6
# characters, and all of which are rendered as uppercase. Barcodes are
# printed as "AB0001L3", where "AB0001" is your label string. They are
# automatically zero-padded, if less than 6 characters.
#
# It can be modified to take more or fewer arguments, though do note
# that the dimensions of the printing must also be adjusted to account
# for this.
#
# usage:
# gen-barcode.sh label1 label2 label3 [output.ps]
if [[ -z "$1" || -z "$2" || -z "$3" ]]
then
echo "Please supply two numbers for the label (107, for example)"
exit 1
else
NUMBER1="$1"
NUMBER2="$2"
NUMBER3="$3"
fi
if [ ! -z "$4" ]
then
OUTPUT="$4"
fi
## Make a padded label from the given number.
function labelof() {
if [ -z "$1" ]
then
echo "Label not given" 1>&2
return 1
elif [[ "$(echo $1 | wc -c )" > 7 ]] # Cannot be more than 6, but includes newline
then
echo "Label too long" 1>&2
return 1
fi
printf "%06dL3" "$1"
}
## Constants for use in generating the barcode.
ENCODING="code39"
LABEL_DIMENSIONS="70x14" # The desired width is 78, so we account for the border.
LABEL_MARGINS="12,2"
TABLE_MARGINS="+0 +0"
PAGE_SIZE="102x58mm"
## Create a temporary file to store the .ps in, while it's converted
## to pdf.
TEMPPS1="/tmp/barcode-1-$$.ps"
TEMPPS2="/tmp/barcode-2-$$.ps"
TEMPPDF="/tmp/barcode-$$.pdf"
set -e
LABEL1="$(labelof $NUMBER1)"
LABEL2="$(labelof $NUMBER2)"
LABEL3="$(labelof $NUMBER3)"
echo "Generating barcode..."
barcode -e "$ENCODING" -u "mm" -g "$LABEL_DIMENSIONS" -p "$PAGE_SIZE" \
-t "1x3 $TABLE_MARGINS" -m "$LABEL_MARGINS" \
-o "$TEMPPS1" -b "$LABEL1" -b "$LABEL2" -b "$LABEL3"
if [ "$?" != 0 ]
then
echo "Barcode generation failed"
exit 1
fi
psnup -b6 "$TEMPPS1" "$TEMPPS2"
echo "Generating PDF..."
ps2pdf "$TEMPPS2" "$TEMPPDF"
if [ ! -z "$OUTPUT" ]
then
cp "$TEMPPS2" "$OUTPUT"
fi
firefox "$TEMPPDF"
rm "$TEMPPS1"
rm "$TEMPPS2"
rm "$TEMPPDF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment