Smile for the Camera: a new cybercrime short story ebook.

10.4 The pst-barcode Package

The pst-barcode package [114] (version 0.12, 2013-10-26, at the time of writing) is a pstricks package for drawing twenty-nine different types of bar codes, including EAN-13 and QR codes. Since this is a pstricks package, it uses PostScript code, which means that it doesn't work directly with PDFLaTeX unless you have the shell escape enabled and use a package such as pdftricks [67].

There are essentially two options if you want to generate a PDF file and you don't have the shell escape enabled:

  1. Use latex, dvips and ps2pdf to obtain a PDF version of the document. For example, if your document is in the file myDoc.tex then you need to run the following commands:

    latex myDoc
    dvips -o myDoc.ps myDoc.dvi
    ps2pdf myDoc.ps myDoc.pdf

    If you use a frontend, such as TeXworks, you need to find the appropriate buttons or menu options to run these commands. If you use arara, you need the following directives:

    % arara: latex
    % arara: dvips
    % arara: ps2pdf

    You can replace the two steps dvips and ps2pdf with a single call to dvipdfm.

  2. Put the pstricks code in a standalone document, compile that document using latex, dvips and ps2pdf, as described above, and include the generated PDF file into the main document using \includegraphics.

The pst-pdf package [63] can be used to simplify the second option if you have multiple pstricks images in your document, but you still need the latex, dvips and ps2pdf invocations.

If I'm designing a flyer that requires a bar code, such as an advance information sheet with a QR code, I usually use the second option. Once I've generated the bar code, I rarely need to change it, as my modifications to the document usually concern the accompanying text rather than the bar code, so it's easiest to treat the bar code as an external graphics file.

The pst-barcode package provides the command:

\psbarcode[options]{text or filename}{PS options}{type}

This generates a bar code with zero size, which means it typically needs to go inside an environment or command where you can specify the height and width. (Recall Volume 1.) Since pst-barcode automatically loads the pstricks package [117], you can use the pspicture environment:

picture commands

As with the picture environment, the co-ordinate arguments are specified using parentheses, but take care as the syntax of the pspicture environment is slightly different to that of the picture environment. In the case of pspicture, the first argument in parentheses (⟨llx⟩,⟨lly⟩) specifies the co-ordinates for the lower left corner, and second argument in parentheses (⟨urx⟩,⟨ury⟩) specifies the co-ordinates for the upper right corner of the picture's bounding box. If (⟨llx⟩,⟨lly⟩) is omitted, the origin is assumed.

The optional argument ⟨options⟩ of \psbarcode is a key=value list. Available options include:

This is a boolean key. This determines whether the argument ⟨text or filename⟩ is the bar code text (in the case of file=false) or the name of the file containing the bar code text (in the case of file=true). The default value for this option is file=false.

This specifies a horizontal shift to apply to the bar code. The default value is transx=0.

This specifies a vertical shift to apply to the bar code. The default value is transy=0.

This specifies a horizontal scaling to apply to the bar code. The default value is scalex=1.

This specifies a vertical scaling to apply to the bar code. The default value is scaley=1.

This specifies the rotation (in degrees) to apply to the bar code. The default value is rotate=0.

The ⟨PS options⟩ argument are PostScript options separated by whitespace. Available options include:

This enables human readable text.

This sets the font, which must be a PostScript font. The default is /Helvetica.

This enables the display of whitespace guard marks.

The final argument ⟨type⟩ of \psbarcode indicates the type of bar code. For example, ean13 for an EAN-13 bar code, isbn for an ISBN bar code, or qrcode for a QR code.

Example 53. ISBN Bar Code

The ISBN bar code is just a special form of EAN-13 bar code with a particular prefix. The data, provided in the ⟨text or filename⟩ argument, should contain 9 or 10 digits for ISBN-10, and 12 or 13 digits for ISBN-13. (In both cases, the digits separated appropriately with hyphens.) If only 9 (ISBN-10) or 12 (ISBN-13) digits are specified the ISBN check digit is calculated automatically.

The ISBN for the paperback version of this book is 978-1-909440-07-4 so I can create the ISBN bar code using:

\psbarcode{1-909440-07-4}{includetext guardwhitespace}{isbn}

Here's a complete document containing the bar code:




\psbarcode{1-909440-07-4}{includetext guardwhitespace}{isbn}


How did I work out the co-ordinates for the bounding box? I put the picture inside the argument of \frame, which marks the picture's extent with a rectangle and then adjusted the co-ordinates until the picture fitted inside the frame. Like this:

\psbarcode{1-909440-07-4}{includetext guardwhitespace}{isbn}

Remember that this example document must be compiled with latex rather than pdflatex. If you want to turn this into an image that you can include in another document, change the document class to standalone [82] and remove all unnecessary blank lines:

% arara: latex
% arara: dvips
% arara: ps2pdf
\psbarcode{1-909440-07-4}{includetext guardwhitespace}{isbn}

(You can download or view this document.)

If the file is called barcode-isbn.tex then you need to run:

latex barcode-isbn
dvips -o barcode-isbn.ps barcode-isbn.dvi
ps2pdf barcode-isbn.ps barcode-isbn.pdf

Alternatively, if you use arara and have included the arara directives shown above, you can just do:

arara barcode-isbn

This creates a PDF file called barcode-isbn.pdf that you can now include in another document using:


This produces:

Image of a barcode with the ISBN above it.

Exercise 28. A QR Code

A QR code is obtained by setting ⟨type⟩ to qrcode. The data setting ⟨text or filename⟩ is typically a website address, so for this exercise, adapt the code from Example 53 so that it's now a QR code. Set the data to a website of your choice. If you can't think of one, you can use this book's home page: http://www.dickimaw-books.com/latex/admin/.

You can download or view a solution to this exercise.

This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs