Hello Everyone:
I’m working on a free/open-source plugin that will generate barcodes. I’m using the Node.js library jsbarcode which also uses the library canvas. I have written the code to produce a barcode as a base64 string. The code is below:
function(properties, context) {
const JsBarcode = require('jsbarcode');
const { Canvas } = require("canvas");
const canvas = new Canvas();
JsBarcode(canvas, "Hello");
let base64String = canvas.toDataURL();
let base64Image = base64String.split(';base64,').pop();
const fs = require('fs');
fs.writeFile('image.png', base64Image, {encoding: 'base64'}, function(err) {
console.log('File created');
});
return {
barcode: "" //need a URL to return ("barcode: String (image URL)")
}
}
The only thing I can’t figure out is how to generate and save a file. I’ve poked around and know it starts with the base64 encoding, which I have above, or file system, which I have too. Any help is greatly appreciated and is going to a free plugin for the community! Thanks!