Resolved fun issues with promises

ref #3
This commit is contained in:
Nicolai Ort 2021-02-02 11:02:23 +01:00
parent 557cc26f28
commit 4617f2c5bd
1 changed files with 31 additions and 3 deletions

View File

@ -1,14 +1,42 @@
import fs from "fs";
import pdf_converter from "html-pdf";
import path from 'path';
/**
* This class is responsible for all things pdf creation.
* This uses the html templates from src/templates.
*/
export class PdFCreator {
export class PdfCreator {
private templateDir = path.join(__dirname, '/templates');
public generateSponsoringContract() {
public async generateSponsoringContract(): Promise<any> {
const template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
let pdf = await pdf_converter.create(template, { format: "A5", orientation: "landscape" });
return await this.toBuffer(pdf);
}
/**
* Promise wrapper function that resolves the toBuffer promise for pdf generation.
* @param pdf The pdf object that shall be turned into a buffer.
*/
public async toBuffer(pdf): Promise<any> {
let promise = await new Promise((resolve, reject) => {
pdf.toBuffer(function (err, buffer) {
resolve(buffer);
});
});
return await promise;
}
/**
* Promise wrapper function that resolves the toStream promise for pdf generation.
* @param pdf The pdf object that shall be turned into a stream.
*/
public async toStream(pdf): Promise<any> {
let promise = await new Promise((resolve, reject) => {
pdf.toStream(function (err, stream) {
resolve(stream);
});
});
return await promise;
}
}