Project

General

Profile

How to produce barcodes

The Velocity Report leverages the powerful Barcode4J Java library to produce high quality barcodes that you can embed in your reports and/or use in your Servoy solution.

You produce a barcode using Servoy to create a simple Javascript Object that you pass to the plugin's method getBarcode().

The signature of this method is:

getBarcode(Scriptable barcodeDefinition, [number resolution]);

This will return a Barcode object that you can use in a report context object or use in a text field of type HTML_AREA.

  • barcodeDefinition is a Scriptable, meaning a simple Javascript Object, where you will stuff all the parameters to define the barcode
  • resolution is an optional number parameter that you can use to specify whether you want to generate a barcode in high resolution (PDF for print) or low (to display on-screen). You can pass one of the constants REPORT.RESOLUTION_LOW or REPORT.RESOLUTION_HIGH!*. Default is REPORT.RESOLUTION_LOW

!* REPORT.RESOLUTION_LOW = 1 and REPORT.RESOLUTION_HIGH = 4

Note that this is in fact an integer factor that is passed to the barcode engine to multiply the screen resolution. The default screen resolution being 96dpi, you get 288dpi for RESOLUTION_HIGH (96x4).

You can pass a different number if you want, but beware of the memory your smart client will need to render high resolution images to PDF - you will have to experiment with this and find the optimal ratio between image quality and memory consumption.

You can set the resolution on the barcode later by using the setResolution(number) method.
You can also retrieve the actual URL of the produced barcode by calling getURL() on the barcode object.

For most PDF reports that you will produce for printing on a computer printer, 288dpi should be enough, and the memory consumption will still be correct (FYI, the max heap size set on the smart client to produce the 12 pages high resolution sample report was 128Mb on Mac OS X with 64-bit Java). The main culprit in terms of memory consumption here is the iText library which is used (also by Jasper Reports) to render to PDF.

Barcode types

There are a number of Barcode types that you can produce with the plugin. The full list is defined in a BARCODE constant object, which you will find in the solution Explorer, under the `VelocityReport` node:
  • CODABAR
  • CODE128
  • CODE39
  • DATAMATRIX
  • EAN128
  • EAN13
  • EAN8
  • INTL2OF5
  • PDF147
  • POSTNET
  • QRCODE
  • ROYALMAIL
  • UPCA
  • UPCE
  • UPCS4CB

Have a look here to see what they look like.

Barcode parameters

You've seen that to create a Barcode object, you need to pass a Javascript object to the plugin's getBarcode() method. Each property of this Javascript Object will be a parameter used to define the resulting barcode.

Creating a barcode definition Object
First you create a Javascript object that will contain all these parameters. You can do it either like this:
var barcodeDef = new Object();

or like that:
var barcodeDef = {};

The end result will be a Javascript Object that will act as a container for all your barcode parameters.

Stuffing the definition Object with barcode parameters

Now you need to add parameters to the definition Object. You do it like this:

barcodeDef.data = 'Servoy Stuff'; 
barcodeDef.type = BARCODE.CODE128; 
barcodeDef.height = 80; 

As you can see, all you need to do is define a property and set its value. That's it!<br/>
Now you really need to know what property names are available, and what values are expected. Here is the full list:

PROPERTY TYPE REQUIRED? DEFAULT INFO
data String (i18n) Y null The actual data accepted will depend on the type of Barcode
type BARCODE Type Y null One of the constants above
height Integer Y 0 Barcode only define height usually, the width being dependent of the data
forcedWidth Integer Y 0 Forces the width to the desired width
forcedHeight Integer Y 0 Forces the height to the desired height
gray Boolean N false Whether to use B&W or Gray level
font String N Arial,Helvetica,Sans-serif Used by the legend
fontSize Integer N 8 Defined in points
moduleWidth String N null A factor applied to modify the width (in mm)
wideFactor Integer N null A different factor that applies to desired width
quietZone String N null Defines an extra space for the legend (in mm or cm or in) - use "disable" if you want to remove it entirely
placement String ('top', 'bottom', 'none') N 'bottom' Where to put the legend - use 'none' if you don't want it
pattern String N null Pattern used to format the legend (uses '_' to define a character, '\\\\' to escape)
id String N null To be able to use CSS specific on the resulting image, you can define an id
format String ('svg', 'png', 'gif', 'jpeg') N 'svg' If you want to use the barcode in a HTML_AREA, use 'png', otherwise the reporting engine will prefer 'svg' - 'png' only available for QRCODE
resolution Integer N REPORT.RESOLUTION_LOW The output resolution factor

Note that for moduleWidth and quietFactor you define a String which is defined in mm or cm, like this:

barcodeDef.moduleWidth = "0.18mm";
barcodeDef.quietZone = "1.2cm";
// or
barcodeDef.quietZone = "0.4in";

You will have to play with those different parameters to achieve the desired result.

The best way to understand these parameters is to use the Barcode4J Servlet tester, which is installed within the Eastwood context.
Just launch you Servoy developer and navigate to http://localhost:8080/eastwood/ (change localhost and 8080 to your respective server name and port) and you will see a test form, which will allow you to try all the parameters above (and immediately see the result in return).

Note to Windows users: do not use Internet Explorer to try this since the page defaults to 'svg' and IE is not capable of showing it - use Firefox instead!