Created a pdf class that takes care of the pdf wrapping

ref #3
This commit is contained in:
Nicolai Ort 2021-02-02 11:30:51 +01:00
parent 64bd1ffc3a
commit 7d5b5750ad
2 changed files with 17 additions and 10 deletions

View File

@ -9,19 +9,27 @@ export class PdfCreator {
private templateDir = path.join(__dirname, '/templates'); private templateDir = path.join(__dirname, '/templates');
//TODO: Accept the runner class //TODO: Accept the runner class
public async generateSponsoringContract(): Promise<any> { public async generateSponsoringContract(): Promise<Pdf> {
let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
template = template.replace("{{Runner Name}}", "lelele"); template = template.replace("{{Runner Name}}", "lelele");
return await pdf_converter.create(template, { format: "A5", orientation: "landscape" }); return new Pdf(await pdf_converter.create(template, { format: "A5", orientation: "landscape" }));
}
}
export class Pdf {
content: any;
constructor(pdf: any) {
this.content = pdf;
} }
/** /**
* Promise wrapper function that resolves the toBuffer promise for pdf generation. * 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> { public async toBuffer(): Promise<any> {
let promise = await new Promise((resolve, reject) => { let promise = await new Promise((resolve, reject) => {
pdf.toBuffer(function (err, buffer) { this.content.toBuffer(function (err, buffer) {
resolve(buffer); resolve(buffer);
}); });
}); });
@ -30,11 +38,10 @@ export class PdfCreator {
/** /**
* Promise wrapper function that resolves the toStream promise for pdf generation. * 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> { public async toStream(): Promise<any> {
let promise = await new Promise((resolve, reject) => { let promise = await new Promise((resolve, reject) => {
pdf.toStream(function (err, stream) { this.content.toStream(function (err, stream) {
resolve(stream); resolve(stream);
}); });
}); });

View File

@ -15,6 +15,6 @@ export class PdfController {
async generateContracts() { async generateContracts() {
//TODO: Accept the real classes //TODO: Accept the real classes
const contracts = await this.pdf.generateSponsoringContract(); const contracts = await this.pdf.generateSponsoringContract();
return await this.pdf.toBuffer(contracts); return await contracts.toBuffer();
} }
} }