template preparation

This commit is contained in:
Julian Krauser 2024-12-23 14:00:50 +01:00
parent 5f18e614be
commit 6cb8ca0a12
16 changed files with 414 additions and 21 deletions

View file

@ -1,36 +1,34 @@
import { readFileSync } from "fs";
import Handlebars from "handlebars";
import puppeteer from "puppeteer";
import { TemplateHelper } from "./templateHelper";
import { PermissionModule } from "../type/permissionTypes";
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;
template: PermissionModule;
title: string;
filename: string;
data: any;
}) {
const templateHtml = this.getTemplate(template);
const templateCompiled = Handlebars.compile(templateHtml);
const html = templateCompiled(data);
const { header, footer, body } = await TemplateHelper.renderFileForModule({
module: template,
bodyData: data,
title: title,
});
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.setContent(body, { waitUntil: "domcontentloaded" });
await page.pdf({
path: process.cwd() + `/export/${filename}.pdf`, // Name der PDF-Datei
path: process.cwd() + `/export/${filename}.pdf`,
format: "A4",
printBackground: false,
margin: {
@ -40,12 +38,8 @@ export abstract class PdfExport {
right: "10mm",
},
displayHeaderFooter: true,
headerTemplate: `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`,
footerTemplate: `
<div style="font-size:10px; text-align:center; width:100%; color:#888;">
Seite <span class="pageNumber"></span> von <span class="totalPages"></span>
</div>
`,
headerTemplate: header,
footerTemplate: footer,
});
await browser.close();