Barcode in report

09-02-2020 10:15

Adding a barcode to a pdf or any kind of other reports is pretty easy since jsreport announced asynchronous helpers.

At first, let's find a proper npm module that can do the work. This can be for example the bwip-js. This module exports asynchronous function bwipjs.toBuffer() returning promise with the image buffer. This is what we want to call in a helper. The Handlebars and also other templating engines don't support asynchronous helpers, but fortunately, jsreport engines sandbox has this covered out of the box and you can safely return a promise from helpers.

The helpers section can look like this:

const bwipjs = require('bwip-js');

function barcode()  {
  return bwipjs.toBuffer({
    bcid:  'code128',
    text:  '0123456789',
    scale:  3,
    height:  10,
    includetext:  true,
    textxalign:  'center',
  }).then(p => p.toString('base64'))
}

Then lets call the helper in the template content:

<img  src='data:image/png;base64,{{barcode}}' />

The result is the barcode image. This works with mostly every recipe. You can add the barcode this way to the docx, pptx or also xlsx reports.

barcode