From a35f8cfd3aa94923968fd77425c074844d28ec0d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Mon, 8 Feb 2021 09:11:59 +0100 Subject: [PATCH] First part of the handlebars barcode generation ref #13 --- src/PdfCreator.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 8c09b3b..d1e17fd 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import bwipjs from "bwip-js"; import cheerio from "cheerio"; import fs from "fs"; import Handlebars from 'handlebars'; @@ -82,6 +83,11 @@ export class PdfCreator { return i18next.t(str, PdfCreator.interpolations).toString(); } ); + await Handlebars.registerHelper('--bc', + function (str) { + return "data:image"; + } + ); this.browser = await puppeteer.launch({ headless: true, args: minimal_args }); } @@ -121,6 +127,9 @@ export class PdfCreator { const $ = cheerio.load(html) $('img').each(async (index, element) => { let imgsrc = $(element).attr("src"); + if (imgsrc.startsWith("data:image")) { + return; + } const img_type = mime.lookup(imgsrc); if (!(img_type.includes("image"))) { @@ -186,7 +195,7 @@ export class PdfCreator { * @param content Barcode content/text - please remember that some formats only support certain input types. * @returns Barcode image as base64 encoded png string. */ - public async generateBarcode(type: string, content: string): Promise { + public static async generateBarcode(type: string, content: string): Promise { let barcode: Buffer = await bwipjs.toBuffer({ bcid: type, text: content,