44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import fs from "fs";
|
|
import path from 'path';
|
|
import puppeteer from "puppeteer";
|
|
import { Runner } from './models/Runner';
|
|
|
|
/**
|
|
* This class is responsible for all things pdf creation.
|
|
* This uses the html templates from src/templates.
|
|
*/
|
|
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(runner: Runner): Promise<any> {
|
|
let template = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
|
|
template = template
|
|
.replace("{{runner_id}}", runner.id.toString())
|
|
.replace("{{runner_firstname}}", runner.firstname)
|
|
.replace("{{runner_lastname}}", runner.lastname)
|
|
.replace("{{runner_groupname}}", runner.group.id.toString());
|
|
return await this.renderPdf(template, { format: "A5", landscape: true });
|
|
}
|
|
|
|
/**
|
|
* 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 renderPdf(html: string, options): Promise<any> {
|
|
const page = await this.browser.newPage();
|
|
await page.setContent(html);
|
|
const pdf = await page.pdf(options);
|
|
await page.close();
|
|
return pdf;
|
|
}
|
|
|
|
} |