PDF Labs


GNU Barcode Plus PDF

Create PDF barcodes using our updated version of the GNU barcode program. Download program files for Windows and Mac or build it from source code.

If you need to create barcodes, then try GNU barcode. It is a command-line tool that can create EAN 13, EAN 8, UPC A, UPC E, ISBN, CODE 128, CODE 128-B, CODE 128-C, CODE 39, CODE 93, CODABAR, MSI and PLESSEY barcodes.

The original version could output these barcodes as PostScript, EPS or PCL. We added an output filter for PDF, which we demonstrate below.

GNU barcode can be used as a stand-alone command-line tool or as a library. It is distributed under a GPL license — please read the author’s README and COPYING files in the zip download.

Get GNU Barcode

This one download has binaries and source code:

()

The Windows binary has been tested on Windows XP, and the Mac binary has been tested on 10.6 (Snow Leopard) — we believe in backward compatibility! Linux users can easily build from source code:

cd src/barcode-0.98.1
./configure
make

Creating PDF Barcodes

You run barcode from the command-prompt like so:

barcode -e 128 -b "foo" -D -o barcode.txt

What are these options? The type of barcode is given after -e, the data to encode is given after -b, the -D means to output PDF, then the output filename goes after -o. Run barcode --help for more:

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>]
   -s <arg>     desired scale for barcode: <s>
   -n           "numeric": avoid printing text along with the bars
   -c           no Checksum character, if the chosen encoding allows it
   -q           omit descriptive comments from output
   -E           print one code as eps file (default: multi-page ps)
   -P           create PCL output instead of postscript
   -D           create PDF 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"
	"isbn"
	"39", "code39"
	"128c", "code128c"
	"128b", "code128b"
	"128", "code128"
	"128raw"
	"i25", "interleaved 2 of 5"
	"cbr", "codabar"
	"msi"
	"pls", "plessey"
	"code93", "93"

Open barcode.txt and you’ll see:

% Printing barcode for "foo", scaled  1.00, encoded using "code 128"
% This is a PDF page description, not a PDF document
% Add to a PDF page stream using a tool such as stamptk
% The space/bar succession is represented by the following widths (space first):
% 02112141124121341111341113111232331112
q
0 g
%   xpos   ypos  width height
   10.00  20.00   1.85  70.00 re
   13.00  20.00   0.85  70.00 re
   16.00  20.00   0.85  70.00 re
   21.00  20.00   0.85  70.00 re
   23.00  20.00   1.85  70.00 re
   29.00  20.00   0.85  70.00 re
   32.00  20.00   0.85  70.00 re
   36.00  20.00   3.85  70.00 re
   41.00  20.00   0.85  70.00 re
   43.00  20.00   0.85  70.00 re
   47.00  20.00   3.85  70.00 re
   52.00  20.00   0.85  70.00 re
   54.00  20.00   2.85  70.00 re
   58.00  20.00   0.85  70.00 re
   60.00  20.00   1.85  70.00 re
   65.00  20.00   1.85  70.00 re
   70.00  20.00   2.85  70.00 re
   74.00  20.00   0.85  70.00 re
   76.00  20.00   1.85  70.00 re
f
BT
/Courier 12.00 Tf
 1 0 0 1  21.00  10.00 Tm (f) Tj
 1 0 0 1  28.30  10.00 Tm (o) Tj
 1 0 0 1  35.60  10.00 Tm (o) Tj
ET
Q
% End barcode for "foo"

This isn’t a PDF document, but rather a PDF page description. This is the code inside of the PDF document that is used to render the page you see. To view it we will need to wrap it in a PDF document.

Here is a quick and dirty way to do that. Download barcode_doc_front.txt and barcode_doc_back.txt to the directory where you created barcode.txt. Next, merge these three files together to make a new PDF. On Windows, use:

type barcode_doc_front.txt barcode.txt barcode_doc_back.txt > barcode_doc.pdf

On Linux or Mac:

cat barcode_doc_front.txt barcode.txt barcode_doc_back.txt > barcode_doc.pdf

Now open barcode_doc.pdf in a PDF viewer. It might complain that the PDF is corrupt (that’s the "dirty" part of this trick), but it should display your barcode in the corner:

barcode for foo

You can edit barcode.pdf in a binary-safe editor such as vim or emacs. It is a good example of basic PDF coding.

You can change the barcode’s location on the page using the geometry option. For example, this will move it one inch up and two inches to the right:

barcode -e 128 -b "foo" -D -o barcode.txt -g+144+72



About PDF Labs

Our mission is to make PDF easier to use.

PDF Labs is operated by Sid Steward, author of PDF Hacks (O’Reilly) and the popular PDF Toolkit.

Please contact Sid Steward by email. Include PDF Labs in the subject to ensure prompt delivery:


About PDF Hacks

PDF Hacks Cover

"If you like pdftk, then you’ll appreciate the other 25 programs and scripts I created for my book, PDF Hacks." –Sid


About PDFtk

For ten years, power users have relied on PDFtk for server-side processing of PDF documents. Our command-line PDFtk has been — and always will be — free (as in freedom) software.

Today we also offer a convenient graphical application: PDFtk Pro. Your purchase of PDFtk Pro will help fund the continued development of our GPL PDFtk.

Thank you for your support!