Grails Cookbook - A collection of tutorials and examples

Grails Barcode (barcode4j) Plugin Example

This example will show how to generate barcodes using the barcode4j plugin for display in web HTML view and in downloaded PDF. The latter is useful for printing on regular or label printer. This example assumes you are using Grails version 2.x.

Install Plugins

The first step is to install barcode4j and rendering plugin by editing grails-app/conf/BuildConfig.groovy.

grails.project.dependency.resolution = {
    ...
    plugins {
        ...
        compile ":barcode4j:0.3"
        compile ":rendering:0.4.4"
    }
}

Note that you don't need the rendering plugin if you only wish to display barcodes on a web page and not on a PDF file.

If you are new to Grails version 2.x and above, the proper way of installing plugins is by using BuildConfig. Usually, after adding entries to BuildConfig, you need to run the application such that the required plugin files are downloaded to the local repository.
$ grails run-app
After the files are downloaded, you can quickly exit the application and begin coding.

Displaying barcode on web browsers

Here is a very simple example on how to display barcodes on a web page. We will display a page that will render the barcode of the numbers: 101, 102, 103, 104 and 105.

Controller code:

package asia.grails.demo
import org.krysalis.barcode4j.impl.code39.Code39Bean
class TestController {
    def web() {
    }
    def showBarcode(String barcode) {
        def generator = new Code39Bean()
        generator.height = 6
        generator.fontSize = 2
        renderBarcodePng(generator, barcode)
    }
}

The web action is the method to invoke to retrieve the page. E.g. http://localhost:8080/grails-barcode/test/web
The showBarcode action contains the logic to render the barcode binary data to the response output stream. Note that:
  • Code 39 is one of the most common barcode format. You can use a different generator to use another format.
  • You can customize the output of the generator by playing with it's properties. Check out the plugin's github project page here: https://github.com/alkemist/grails-barcode4j

web.gsp code:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<g:each in="${101..105}" var="number">
    <div>This is the barcode for the number: ${number}</div>
    <div><g:img dir="test" file="showBarcode?barcode=${number}"/></div>
    <hr/>
</g:each>
</body>
</html>

The gsp page just calls g.img multiple times passing different values. The showBarcode controller action is invoked for each barcode value.
The expected output looks like this on a web browser:
barcode

Displaying barcode on PDF

We usually have no use of barcodes displayed on a web page. It is mostly intended for printing to be read later with a barcode scanner. PDF file is one of the most ideal format for printing purposes.
Here is an example on how to make a PDF file for download that contains some barcodes. This will have the same output as the web code above, only applied for downloaded PDF.
Controller code:
package asia.grails.demo
import org.krysalis.barcode4j.impl.code39.Code39Bean
class TestController {
    def barcode4jService
    ...
    def download() {
        response.contentType = 'application/pdf'
        response.setHeader("Content-disposition", "attachment; filename=\"barcode-example.pdf\"")
        def generator = new Code39Bean()
        generator.height = 6
        generator.fontSize = 2
        def items = []
        (101..105).each { number->
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            barcode4jService.render(generator, number.toString(), bos, "image/png")
            items << [number:number, bytes:bos.toByteArray()]
        }
        renderPdf (template:'download', model:[items:items])
    }
}

The file barcode-example.pdf will be downloaded when the action is invoked. E.g. when this URL is accessed: http://localhost:8080/grails-barcode/test/download
  • The response.setHeader section will tell the browser that the incoming downloaded file is named barcode-example.pdf. The response.contentType on the other hand tells the browser about the mime type of the file.
  • In contrast to the web output code above, the download code will prepare all the 5 barcode binaries in one go. It will save all information on a list (items) with pair of information: number and barcode binary.
  • renderPdf is a helper from the rendering plugin to render a GSP page to a PDF file. The _download.gsp file is invoked passing the item list information generated.

_download.gsp

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<body>
<g:each in="${items}" var="item">
    <div>This is the barcode for the number: ${item.number}</div>
    <div><rendering:inlineJpeg bytes="${item.bytes}" width="170"/></div>
    <hr/>
</g:each>
</body>
</html>

The items are looped inside the GSP. The barcode binaries are rendered using the tag library from the rendering plugin. Note that the code <g:img dir="test" file="showBarcode?barcode=${item.number}"/> can not be used instead of <rendering:inlineJpeg bytes="${item.bytes}" width="170"/> due to some limitations with the rendering plugin.
Here is an example of the output PDF file generated byr the code above: barcode-example.pdf

Code

The full source code for this example can be viewed here or can be downloaded here.

Other Grails Plugins Tutorials


Tags: barcode, Plugin