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();

View file

@ -0,0 +1,76 @@
import { readFileSync } from "fs";
import TemplateService from "../service/templateService";
import { PermissionModule } from "../type/permissionTypes";
import TemplateUsageService from "../service/templateUsageService";
import Handlebars from "handlebars";
export abstract class TemplateHelper {
static getTemplateFromFile(template: string) {
return readFileSync(`${process.cwd()}/src/templates/${template}.template.html`, "utf8");
}
static async getTemplateFromStore(templateId: number): Promise<string> {
return (await TemplateService.getById(templateId)).html;
}
static applyDataToTemplate(template: string, data: any): string {
const normalizedTemplate = this.normalizeTemplate(template);
const templateCompiled = Handlebars.compile(normalizedTemplate);
return templateCompiled(data);
}
static normalizeTemplate(template: string): string {
template = template.replace(/<listend>.*?<\/listend>/g, "{{/each}}");
template = template.replace(/<liststart\b[^>]*>(WDH Start: )?/g, "{{#each ");
template = template.replace(/<\/liststart>/g, "}}");
return template;
}
static async renderFileForModule({
module,
title = "pdf-export Mitgliederverwaltung",
headerData = {},
bodyData = {},
footerData = {},
}: {
module: PermissionModule;
title?: string;
headerData?: any;
bodyData?: any;
footerData?: any;
}): Promise<{ header: string; body: string; footer: string }> {
const moduleTemplates = await TemplateUsageService.getByScope(module);
let header = `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`;
let footer = `
<div style="font-size:10px; text-align:center; width:100%; color:#888;">
Seite <span class="pageNumber"></span> von <span class="totalPages"></span>
</div>
`;
let body = "";
if (moduleTemplates.headerId) {
header = await this.getTemplateFromStore(moduleTemplates.headerId);
header = this.applyDataToTemplate(header, headerData);
}
if (moduleTemplates.footerId) {
footer = await this.getTemplateFromStore(moduleTemplates.footerId);
footer = this.applyDataToTemplate(footer, footerData);
}
if (moduleTemplates.bodyId) {
body = await this.getTemplateFromStore(moduleTemplates.bodyId);
} else {
body = this.getTemplateFromFile(module);
}
body = this.applyDataToTemplate(body, bodyData);
return {
header,
footer,
body,
};
}
}