parent
4187a8e820
commit
edc846ab05
@ -40,6 +40,7 @@
|
||||
"license": "CC-BY-NC-SA-4.0",
|
||||
"dependencies": {
|
||||
"@odit/class-validator-jsonschema": "^2.1.1",
|
||||
"async-helpers": "^0.3.17",
|
||||
"axios": "^0.21.1",
|
||||
"bwip-js": "^2.0.12",
|
||||
"cheerio": "^1.0.0-rc.5",
|
||||
|
@ -1,5 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import bwipjs from "bwip-js";
|
||||
import cheerio from "cheerio";
|
||||
import fs from "fs";
|
||||
import Handlebars from 'handlebars';
|
||||
@ -9,6 +8,7 @@ import mime from "mime-types";
|
||||
import path from 'path';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import puppeteer from "puppeteer";
|
||||
import { awaitAsyncHandlebarHelpers, helpers } from './asyncHelpers';
|
||||
import { Runner } from './models/Runner';
|
||||
import { RunnerGroup } from './models/RunnerGroup';
|
||||
|
||||
@ -78,18 +78,13 @@ export class PdfCreator {
|
||||
loadPath: path.join(__dirname, '/locales/{{lng}}.json')
|
||||
}
|
||||
});
|
||||
|
||||
await Handlebars.registerHelper(helpers);
|
||||
await Handlebars.registerHelper('__',
|
||||
function (str) {
|
||||
return i18next.t(str, PdfCreator.interpolations).toString();
|
||||
}
|
||||
);
|
||||
await Handlebars.registerHelper('--bc',
|
||||
function (str) {
|
||||
//TODO: Fix the async behaviour
|
||||
return "data:image"
|
||||
return PdfCreator.generateBarcode("code39", str.toString());
|
||||
}
|
||||
);
|
||||
this.browser = await puppeteer.launch({ headless: true, args: minimal_args });
|
||||
}
|
||||
|
||||
@ -115,7 +110,8 @@ export class PdfCreator {
|
||||
await i18next.changeLanguage(locale);
|
||||
const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
|
||||
const template = Handlebars.compile(template_source);
|
||||
const result = template({ runners })
|
||||
let result = template({ runners });
|
||||
result = await awaitAsyncHandlebarHelpers(result);
|
||||
const pdf = await this.renderPdf(result, { format: "A5", landscape: true });
|
||||
return pdf
|
||||
}
|
||||
@ -190,25 +186,6 @@ 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 static 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.
|
||||
|
32
src/asyncHelpers.ts
Normal file
32
src/asyncHelpers.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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)
|
||||
});
|
||||
});
|
||||
}
|
@ -54,7 +54,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="column">
|
||||
<img width="100%" style="vertical-align: revert; margin-top: auto;" src="{{--bc this.id}}" />
|
||||
<img width="100%" style="vertical-align: revert; margin-top: auto;" src="{{--bc this.id 'code39'}}" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="columns" style="padding-top: 1rem;">
|
||||
|
Loading…
x
Reference in New Issue
Block a user