Compare commits

..

3 commits

3 changed files with 102 additions and 60 deletions

View file

@ -61,39 +61,41 @@ export abstract class NewsletterHelper {
newsletterTitle: newsletter.newsletterTitle,
newsletterText: newsletter.newsletterText,
newsletterSignatur: newsletter.newsletterSignatur,
dates: dates.map((d) => ({
title: d.diffTitle || d.calendar.title,
content: d.diffDescription || d.calendar.content,
starttime: d.calendar.starttime,
formattedStarttime: new Date(d.calendar.starttime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
}),
formattedFullStarttime: new Date(d.calendar.starttime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
}),
endtime: d.calendar.endtime,
formattedEndtime: new Date(d.calendar.endtime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
}),
formattedFullEndtime: new Date(d.calendar.endtime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
}),
location: d.calendar.location,
})),
dates: dates
.map((d) => ({
title: d.diffTitle || d.calendar.title,
content: d.diffDescription || d.calendar.content,
starttime: d.calendar.starttime,
formattedStarttime: new Date(d.calendar.starttime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
}),
formattedFullStarttime: new Date(d.calendar.starttime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
}),
endtime: d.calendar.endtime,
formattedEndtime: new Date(d.calendar.endtime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
}),
formattedFullEndtime: new Date(d.calendar.endtime).toLocaleDateString("de-DE", {
weekday: "long",
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
}),
location: d.calendar.location,
}))
.sort((a, b) => a.starttime.getTime() - b.starttime.getTime()),
...(recipient
? {
recipient: {
@ -280,7 +282,7 @@ export abstract class NewsletterHelper {
const pdfRecipients = await this.getPrintRecipients(newsletterId);
this.formatJobEmit("progress", "pdf", "info", newsletterId, pdfRecipients.length, 0, "starting sending");
this.formatJobEmit("progress", "pdf", "info", newsletterId, pdfRecipients.length, 0, "starting printing");
for (const [index, rec] of pdfRecipients.entries()) {
let data = this.buildData(newsletter, dates, rec, printWithAdress.includes(rec.sendNewsletter?.type?.id));

View file

@ -1,10 +1,47 @@
import puppeteer from "puppeteer";
import puppeteer, { Browser } from "puppeteer";
import { TemplateHelper } from "./templateHelper";
import { PermissionModule } from "../type/permissionTypes";
import { FileSystemHelper } from "./fileSystemHelper";
import { PDFDocument } from "pdf-lib";
import { StringHelper } from "./stringHelper";
export abstract class PdfExport {
private static browserInstance: undefined | Browser = undefined;
private static timeout: undefined | NodeJS.Timeout = undefined;
private static printing = new Map<string, string>();
private static async renderTemplate(
template: `${PermissionModule}` | `${PermissionModule}.${string}`,
title: string,
data: any,
customTemplate?: {
headerId?: number;
footerId?: number;
bodyId?: string | number;
headerHeight: number;
footerHeight: number;
}
): Promise<{ header: string; footer: string; body: string; headerMargin?: number; footerMargin?: number }> {
if (!customTemplate) {
return await TemplateHelper.renderFileForModule({
module: template,
headerData: data,
bodyData: data,
footerData: data,
title: title,
});
} else {
return await TemplateHelper.renderFileForCustom({
module: template,
customTemplate,
headerData: data,
bodyData: data,
footerData: data,
title: title,
});
}
}
static async renderFile({
template,
title = "pdf-export FF Admin",
@ -28,33 +65,25 @@ export abstract class PdfExport {
footerHeight: number;
};
}) {
try {
clearTimeout(this.timeout);
} catch (err) {}
let id = StringHelper.random(32);
this.printing.set(id, "printing");
if (folder != "") FileSystemHelper.createFolder(folder);
let header: string, footer: string, body: string, headerMargin: number, footerMargin: number;
if (!customTemplate) {
({ header, footer, body, headerMargin, footerMargin } = await TemplateHelper.renderFileForModule({
module: template,
headerData: data,
bodyData: data,
footerData: data,
title: title,
}));
} else {
({ header, footer, body, headerMargin, footerMargin } = await TemplateHelper.renderFileForCustom({
module: template,
customTemplate,
headerData: data,
bodyData: data,
footerData: data,
title: title,
}));
const renderedTemplate = await this.renderTemplate(template, title, data, customTemplate);
let { header, footer, body, headerMargin, footerMargin } = renderedTemplate;
if (!this.browserInstance || !this.browserInstance.connected) {
this.browserInstance = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-gpu", "--disable-setuid-sandbox"],
});
}
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-gpu", "--disable-setuid-sandbox"],
});
const page = await browser.newPage();
const page = await this.browserInstance.newPage();
await page.setContent(body, { waitUntil: "domcontentloaded" });
const exportPath = FileSystemHelper.formatPath(folder, `${filename}.pdf`);
@ -74,7 +103,16 @@ export abstract class PdfExport {
footerTemplate: footer,
});
await browser.close();
await page.close();
this.printing.delete(id);
if (this.printing.size == 0) {
this.timeout = setTimeout(() => {
this.browserInstance.close();
this.browserInstance = undefined;
}, 5000);
}
return pdf;
}

View file

@ -11,7 +11,9 @@ export abstract class TemplateHelper {
try {
tmpFile = FileSystemHelper.readTemplateFile(`/src/templates/${template}.template.html`);
} catch (err) {
tmpFile = FileSystemHelper.readTemplateFile(`/src/templates/${template.split(".")[1]}.template.html`);
tmpFile = FileSystemHelper.readTemplateFile(
`/src/templates/${template.split(".")[template.split(".").length - 1]}.template.html`
);
}
return tmpFile;
}