newsletter CRUD & pdf
This commit is contained in:
parent
e9b29f8acf
commit
01ce3fdd39
31 changed files with 1185 additions and 23 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue