import { readFileSync } from "fs"; import Handlebars from "handlebars"; import puppeteer from "puppeteer"; export abstract class PdfExport { static getTemplate(template: string) { return readFileSync(process.cwd() + "/src/templates/" + template, "utf8"); } static async renderFile({ template, title = "pdf-export Mitgliederverwaltung", filename, data, }: { template: string; title: string; filename: string; data: any; }) { const templateHtml = this.getTemplate(template); const templateCompiled = Handlebars.compile(templateHtml); const html = templateCompiled(data); const browser = await puppeteer.launch({ headless: true, args: ["--no-sandbox", "--disable-gpu", "--disable-setuid-sandbox"], }); const page = await browser.newPage(); await page.setContent(html, { waitUntil: "domcontentloaded" }); await page.pdf({ path: process.cwd() + `/export/${filename}.pdf`, // Name der PDF-Datei format: "A4", printBackground: false, margin: { top: "15mm", bottom: "15mm", left: "10mm", right: "10mm", }, displayHeaderFooter: true, headerTemplate: `

${title}

`, footerTemplate: `
Seite von
`, }); await browser.close(); } }