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.