From 8072d0b1940ef6f316ce78dcbcb9e5af5bab04e7 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Mon, 8 Feb 2021 08:32:39 +0100 Subject: [PATCH] Added basic barcode generation ref #13 --- package.json | 1 + src/PdfCreator.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/package.json b/package.json index 90132ef..effbbb9 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dependencies": { "@odit/class-validator-jsonschema": "^2.1.1", "axios": "^0.21.1", + "bwip-js": "^2.0.12", "cheerio": "^1.0.0-rc.5", "class-transformer": "0.3.1", "class-validator": "^0.13.1", diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index df60730..8c09b3b 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -179,6 +179,25 @@ export class PdfCreator { return (await mergedPdf.save()); } + /** + * A simple barcode generation function using default params suitable for usage in pdfs. + * Generates the barcodes as base64-encoded png. + * @param type Barcode type according to this list: https://github.com/metafloor/bwip-js/wiki/BWIPP-Barcode-Types + * @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 { + 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')}`; + } + /** * Generates a new dummy runner with halfspaces for all strings. * Can be used to generate empty sponsoring contracts.