Roland's homepage

My random knot in the Web

Generating barcodes with “BWIPP

Originally I looked for a Python module to generate barcodes, and I found the treepoem module. This used Pillow to create bitmaps, and I preferred to have PDF or other resolution independant formats. Looking through the README, I found that treepoem is a wrapper for bwipp. This is a set of PostScript procedures to generate barcodes.

Since I’m familiar with using PostScript, I decided to use bwipp directly.

I downloaded “postscriptbarcode-monolithic-2020-04-01.tgz” from it github releases, and unpacked it. See also the bwipp website for documentation. Then I copied the to ${HOME}/lib/ps. (That location is part of my GS_LIB environment variable.)

> cd tmp/src/
> tar xf ../postscriptbarcode-monolithic-2020-04-01.tgz
> cd postscriptbarcode-2020-04-01/monolithic/
> cp barcode.ps ~/lib/ps/

The following PostScript code generates a large (1” square) GS1 datamatrix barcode in a tightly fitting page.

%!PS-Adobe-3.0
%%BoundingBox: -1 0 73 72
% file: GS1-datamatrix.ps
% vim:fileencoding=ISO-8859-1:fdm=marker:ft=postscr
%
% Copyright © 2019 R.F. Smith <rsmith@xs4all.nl>
% Created: 2019-07-05T22:17:59+0200
% Last modified: 2020-05-10T21:16:01+0200
% Convert to PDF with the following command.
%   gs -q -sDEVICE=pdfwrite -dNOPAUSE -dNOSAFER -dAutoRotatePages=/None -dBATCH \
%   -sOutputFile=out.pdf in.eps
%%EndComments
(barcode.ps) runlibfile
%%EndProlog
%%BeginSetup
    << /PageSize [72 72] >> setpagedevice
%%EndSetup
%%Page: 1 1

0 0 moveto
((90)591480047505) (width=1 height=1) /gs1datamatrix /uk.co.terryburton.bwipp findresource exec
showpage
quit

The following command is used to convert this to a PDF file, and to the PNG file shown below.

gs -q -sDEVICE=pdfwrite -dNOPAUSE -dNOSAFER -dAutoRotatePages=/None -dBATCH \
-sOutputFile=dm.pdf dm.ps
gs -q -sDEVICE=pngmono -dNOPAUSE -dNOSAFER -dAutoRotatePages=/None -dBATCH \
-sOutputFile=dm.png dm.ps

This is what the barcode looks like.

Datamatrix barcode for “(90)591480047505”.

N.B: Installing BWIPP as a resource didn’t work for me. If I installed the resouces in /home/rsmith/lib/ps/Resource, ghostscript couldn’t find it with GS_LIB set to /home/rsmith/lib/ps. This seems to be in contradiction with the ghostscript website. When I set GS_LIB to /home/rsmith/lib/ps/Resource, BWIPP would work. But other postscript programs that used e.g. standard resources would not work!.


Related articles


←  A simple feed reader for Youtube