32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import AsyncHelpers from "async-helpers";
|
|
import bwipjs from "bwip-js";
|
|
|
|
export const asyncHelpers = new AsyncHelpers();
|
|
async function generateBarcode(str, options, emtpy, cb) {
|
|
let res = await generateBase64Barcode(options.toString(), str.toString());
|
|
cb(null, res);
|
|
}
|
|
generateBarcode.async = true;
|
|
asyncHelpers.set('--bc', generateBarcode);
|
|
export const helpers = asyncHelpers.get({ wrap: true });
|
|
|
|
export async function generateBase64Barcode(type: string, content: string): Promise<String> {
|
|
let barcode: Buffer = await bwipjs.toBuffer({
|
|
bcid: type,
|
|
text: content,
|
|
scale: 3,
|
|
height: 10,
|
|
includetext: true,
|
|
textxalign: 'center',
|
|
});
|
|
return `data:image/png;base64,${barcode.toString('base64')}`;
|
|
}
|
|
|
|
export async function awaitAsyncHandlebarHelpers(input: string): Promise<string> {
|
|
return await new Promise((resolve, reject) => {
|
|
asyncHelpers.resolveIds(input, (err, data) => {
|
|
if (err) { reject(err) }
|
|
resolve(data)
|
|
});
|
|
});
|
|
} |