diff --git a/package.json b/package.json index ba89b3e..1a028db 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", - "html-pdf": "^2.2.0", + "puppeteer": "^7.0.1", "reflect-metadata": "^0.1.13", "routing-controllers": "^0.9.0-alpha.6", "routing-controllers-openapi": "^2.2.0" @@ -54,6 +54,7 @@ "@odit/license-exporter": "^0.0.9", "@types/express": "^4.17.11", "@types/node": "^14.14.22", + "@types/puppeteer": "^5.4.3", "nodemon": "^2.0.7", "release-it": "^14.2.2", "rimraf": "^3.0.2", @@ -73,4 +74,4 @@ "publish": false } } -} +} \ No newline at end of file diff --git a/src/PdfCreator.ts b/src/PdfCreator.ts index 2345c8c..f531a45 100644 --- a/src/PdfCreator.ts +++ b/src/PdfCreator.ts @@ -1,7 +1,6 @@ import fs from "fs"; -import pdf_converter from "html-pdf"; import path from 'path'; -import { Stream } from 'stream'; +import puppeteer from "puppeteer"; /** * This class is responsible for all things pdf creation. @@ -9,48 +8,32 @@ import { Stream } from 'stream'; */ export class PdfCreator { private templateDir = path.join(__dirname, '/templates'); + private browser; + + constructor() { + puppeteer.launch({ headless: true }).then(browser => { + this.browser = browser; + }); + } //TODO: Accept the runner class - public async generateSponsoringContract(): Promise { + public async generateSponsoringContract(): Promise { let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); template = template.replace("{{Runner Name}}", "lelele"); - return new Pdf(await pdf_converter.create(template, { format: "A5", orientation: "landscape" })); - } - -} - -/** - * This class is a wrapper for the pdf objects created by html-pdf. - * It offers typed conversion to Buffer and Stream. - */ -export class Pdf { - content: any; - - constructor(pdf: any) { - this.content = pdf; + return await this.renderPdf(template, { format: "A5", landscape: true }); } /** - * Promise wrapper function that resolves the toBuffer promise for pdf generation. + * This method manages the creation of pdfs via puppeteer. + * @param html The HTML that should get rendered. + * @param options Puppeteer PDF option (eg: {format: "A4"}) */ - public async toBuffer(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toBuffer(function (err, buffer: Buffer) { - resolve(buffer); - }); - }); - return await promise; + public async renderPdf(html: string, options): Promise { + const page = await this.browser.newPage(); + await page.setContent(html); + const pdf = await page.pdf(options); + await page.close(); + return pdf; } - /** - * Promise wrapper function that resolves the toStream promise for pdf generation. - */ - public async toStream(): Promise { - let promise = await new Promise((resolve, reject) => { - this.content.toStream(function (err, stream: Stream) { - resolve(stream); - }); - }); - return await promise; - } } \ No newline at end of file diff --git a/src/controllers/PdfController.ts b/src/controllers/PdfController.ts index 0ec9581..41d2c4a 100644 --- a/src/controllers/PdfController.ts +++ b/src/controllers/PdfController.ts @@ -15,6 +15,6 @@ export class PdfController { async generateContracts() { //TODO: Accept the real classes const contracts = await this.pdf.generateSponsoringContract(); - return await contracts.toBuffer(); + return await contracts; } } \ No newline at end of file