how to convert number to bar code using cloudfunction and nodejs

690 views Asked by At

how to implement this jsbarcode npm on convert phone or mobile number to bar code using cloud function and nodejs

var jsbarcode = require("jsbarcode")
<img id="barcode"/>
$("#barcode").JsBarcode("Hi!");
JsBarcode("#barcode", "1234", {
  format: "pharmacode",
  lineColor: "#0aa",
  width:4,
  height:40,
  displayValue: false
});
1

There are 1 answers

0
Pushkin On

For using JsBarcode in NodeJS, you need canvas (node-canvas), not HTML image tag.

Install canvas

npm install canvas

Then use it with jsbarcode

const JsBarcode = require('jsbarcode');
const { Canvas } = require("canvas");

const number = getNumberFromWherever()

// Create a new canvas
const canvas = new Canvas();
JsBarcode(canvas, number, {
  format: "pharmacode",
  lineColor: "#0aa",
  width:4,
  height:40,
  displayValue: false
});

// Get Data URL and save it to the DB

canvas.toDataURL('image/png', (err, png) => {
  // Do whatever you want to do
  // for storing in the DB
})

Please refer node-canvas GitHub page for more details.