Added basic barcode generation

ref #13
This commit is contained in:
Nicolai Ort 2021-02-08 08:32:39 +01:00
parent a30600943d
commit 8072d0b194
2 changed files with 20 additions and 0 deletions

View File

@ -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",

View File

@ -179,6 +179,25 @@ export class PdfCreator {
return <Buffer>(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<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')}`;
}
/**
* Generates a new dummy runner with halfspaces for all strings.
* Can be used to generate empty sponsoring contracts.