Skip to main content

Command Palette

Search for a command to run...

Today's npm package: bwip-js

Updated
โ€ข2 min read
Today's npm package: bwip-js
P

I am developer from India currently based in Berlin, Germany.

I am very interested in Design, though I don't design ๐Ÿ˜‰ I take care of it while making my apps.

Photography, videography and having random conversation over beer are my activities of spare time. Timelapses and hyperlapses make me wonder all the time ๐Ÿ˜Š

Today's npm package is bwip-js

Text based input from human to computer is one way we can introduce real world to the Computer applications.

Printed data like Barcodes allow faster input of data via Optical data reading.

BwipJS (Barcode Writer in Pure JavaScript) is a JS implementation of Barcode Writer in Pure JavaScript and allows usage as a JS lib, CLI etc.

Let's see how it works:

QR Code

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

const pngFromBase64 = (data) => `data:image/png;base64,${data}`;

bwipjs.toBuffer({
  bcid: 'qrcode', // Barcode type
  text: '0123456789', // Text to encode
  scale: 6, // 6x scaling factor
  textxalign: 'center', // Always good to set this
}, (err, data) => {
  if (err) {
     throw err;
  }

  const bufferBase64 = data.toString('base64');

  const img = `<img src="${pngFromBase64(bufferBase64)}" />`;

  console.log(img);
});

Or same code as promise:

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

const pngFromBase64 = (data) => `data:image/png;base64,${data}`;

bwipjs.toBuffer({
  bcid: 'qrcode', // Barcode type
  text: '0123456789', // Text to encode
  scale: 6, // 6x scaling factor
  textxalign: 'center', // Always good to set this
}).then((data) => {  
  const bufferBase64 = data.toString('base64');

  return `<img src="${pngFromBase64(bufferBase64)}" />`;
}).catch(err => (throw err));

qrcode.png

See it in action here: https://runkit.com/pankaj/bwip-js

Barcode

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

const pngFromBase64 = (data) => `data:image/png;base64,${data}`;

bwipjs.toBuffer({
  bcid: 'code128', // Barcode type
  text: '0123456789', // Text to encode
  scale: 6, // 6x scaling factor
  textxalign: 'center', // Always good to set this
}, (err, data) => {
  if (err) {
     throw err;
  }

  const bufferBase64 = data.toString('base64');

  const img = `<img src="${pngFromBase64(bufferBase64)}" />`;

  console.log(img);
});

barcode.png

See the barcode in action here: https://runkit.com/pankaj/bwip-js-barcode

Stay tuned for tomorrow's package of the Day.

More from this blog

P

Pankaj Patel - @heypankaj_

63 posts

Sharing my learnings and thoughts on Blogging, Personal Branding and Programming