newsletter CRUD & pdf

This commit is contained in:
Julian Krauser 2024-12-25 12:22:28 +01:00
parent e9b29f8acf
commit 01ce3fdd39
31 changed files with 1185 additions and 23 deletions

View file

@ -0,0 +1,31 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { readdirSync } from "fs";
export abstract class FileSystemHelper {
static createFolder(newFolder: string) {
const exportPath = join(process.cwd(), "export", newFolder);
if (!existsSync(exportPath)) {
mkdirSync(exportPath, { recursive: true });
}
}
static readFile(filePath: string) {
return readFileSync(join(process.cwd(), filePath), "utf8");
}
static writeFile(filePath: string, file: any) {
writeFileSync(filePath, file);
}
static formatPath(...args: string[]) {
return join(...args);
}
static getFilesInDirectory(directoryPath: string, filetype?: string): string[] {
const fullPath = join(process.cwd(), directoryPath);
return readdirSync(fullPath, { withFileTypes: true })
.filter((dirent) => !dirent.isDirectory() && (!filetype || dirent.name.endsWith(filetype)))
.map((dirent) => dirent.name);
}
}

View file

@ -1,6 +1,8 @@
import puppeteer from "puppeteer";
import { TemplateHelper } from "./templateHelper";
import { PermissionModule } from "../type/permissionTypes";
import { FileSystemHelper } from "./fileSystemHelper";
import { PDFDocument } from "pdf-lib";
export abstract class PdfExport {
static async renderFile({
@ -10,6 +12,7 @@ export abstract class PdfExport {
data = {},
saveToDisk = true,
margins = { top: "15mm", bottom: "15mm" },
folder = "",
}: {
template: PermissionModule;
title?: string;
@ -17,7 +20,10 @@ export abstract class PdfExport {
data?: any;
saveToDisk?: boolean;
margins?: { top: string; bottom: string };
folder?: string;
}) {
if (folder != "") FileSystemHelper.createFolder(folder);
const { header, footer, body } = await TemplateHelper.renderFileForModule({
module: template,
bodyData: data,
@ -31,8 +37,10 @@ export abstract class PdfExport {
const page = await browser.newPage();
await page.setContent(body, { waitUntil: "domcontentloaded" });
const exportPath = FileSystemHelper.formatPath(process.cwd(), "export", folder, `${filename}.pdf`);
let pdf = await page.pdf({
...(saveToDisk ? { path: process.cwd() + `/export/${filename}.pdf` } : {}),
...(saveToDisk ? { path: exportPath } : {}),
format: "A4",
printBackground: false,
margin: {
@ -50,4 +58,25 @@ export abstract class PdfExport {
return pdf;
}
static async sqashToSingleFile(inputFolder: string, outputFile: string, outputFolder: string = "") {
if (outputFolder != "") FileSystemHelper.createFolder(outputFolder);
let pdfFilePaths = FileSystemHelper.getFilesInDirectory(inputFolder, ".pdf");
const mergedPdf = await PDFDocument.create();
for (const pdfPath of pdfFilePaths) {
const pdfBytes = FileSystemHelper.readFile(pdfPath);
const pdf = await PDFDocument.load(pdfBytes);
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
copiedPages.forEach((page) => mergedPdf.addPage(page));
}
const mergedPdfBytes = await mergedPdf.save();
const exportPath = FileSystemHelper.formatPath(process.cwd(), "export", outputFolder, `${outputFile}.pdf`);
FileSystemHelper.writeFile(exportPath, mergedPdfBytes);
}
}

View file

@ -1,12 +1,12 @@
import { readFileSync } from "fs";
import TemplateService from "../service/templateService";
import { PermissionModule } from "../type/permissionTypes";
import TemplateUsageService from "../service/templateUsageService";
import Handlebars from "handlebars";
import { FileSystemHelper } from "./fileSystemHelper";
export abstract class TemplateHelper {
static getTemplateFromFile(template: string) {
return readFileSync(`${process.cwd()}/src/templates/${template}.template.html`, "utf8");
return FileSystemHelper.readFile(`/src/templates/${template}.template.html`);
}
static async getTemplateFromStore(templateId: number): Promise<string> {