Barcodes Galore

So you may need to include a barcode on some reports or forms that you’re generating. Or you might want to include a QR code for your organisation’s newsletter or flyer, to indicate a URL, or an event. Or even one for your personal contact details on your business card.

You can, of course, seek out a barcode generator to get a PNG of your barcode to be included in your document. But did you know about the pst-barcode package that’ll do the job nicely?

Example Barcodes

I mentioned the ean13isbn package in a previous post, but that package is only for generating ISBN barcodes. pst-barcode, on the other hand, can generate many different types of barcodes. Some simple examples below, taken from the pst-barcode package documentation. Remember to \usepackage{pst-barcode}.

\psbarcode{^453^178^121^239}{columns=2 rows=10}{pdf417}
\psbarcode{Herbert Voss ^142^164^186}
{rows=48 columns=48 parse}{datamatrix}


Encode Almost Anything with QR Codes

QR barcodes can be used to encode a wide range of data. You can use a camera phones to scan and decode them using a reader application (e.g. ZXing, Kaywa, Qrafter, etc.). (You can also use the ZXing online barcode decoder for testing purposes.) Depending on the decoded data, specific actions will be triggered.

For example, the QR code above encodes the URL of this blog. Scan it with a camera phone, and an Internet browser will open and load this blog automatically.

The following code encodes my contact details as a MeCard. After scanning it, a good barcode reader application will automatically decode the details and prompt you to add my contact details to your address book. (BTW my address and phone number in this barcode is fake.)

% Do not put newlines in the MECARD text
\psbarcode{MECARD:N:Lian Tze,Lim;TEL:+60123456789;EMAIL:liantze\;ADR:,109 Lorong Oak 47,Taman Oak,Butterworth,Pulau Pinang,13000,Malaysia;URL:;;}
{eclevel=M height=2 width=2}{qrcode}

It seems MeCard is used more widely than vCard for QR codes on business cards, but if you want a vCard anyway, here’s the code. vCard does allow you to add a photo, which is nice. Note that you need to represent newline characters with ^^J for the code to compile.

% Line breaks occur only after ^^J%
N:Lian Tze;Lim^^J%
FN:Lim Lian Tze^^J%
ADR:;;109 Lorong Oak 47, Taman Oak;Butterworth;Pulau Pinang;13000;Malaysia^^J%
{eclevel=M height=2 width=2}{qrcode}

You can also add a barcode encoding an iCalendar for an event, e.g. in a newsletter. A good reader app would be able to add the details automatically to your calendar. (Qrafter for the iOS worked for me.)

% Line breaks occur only after ^^J%
SUMMARY:25th Pacific Asia Conference on Language, Information and Computation^^J%
ORGANIZER;CN=Assoc Prof Francis^^J%
LOCATION:Nanyang Technological University, Singapore^^J%
{eclevel=M height=2 width=2}{qrcode}

What If I’m Generating to PDF directly?

If you’re using xelatex, apparently all the pstricks packages work smoothly and you get a flawless PDF.

If you’re using pdflatex, you can still use pst-barcode (and other pstricks packages) with a little extra bit of work. Here’s what your preamble should include:


You will also need to install pdfcrop, available from CTAN; install with your TeXLive/MikTeX package manager). This is actually a Perl script that crops away extra whitespace around each page of a PDF file.

You must add the -shell-escape option when running pdflatex for this to work. What happens is that a ps file is generated and converted to a PDF on-the-fly, then cropped and included in the final output PDF file. Understandably, the whole process may take a bit longer than usual.

Hopefully this’ll help anyone who’s looking to batch generate reports or forms that need a barcode or two!