From f09135319a5614ef10274d900d704609e078858b Mon Sep 17 00:00:00 2001 From: TheThomaas Date: Sun, 8 Jun 2025 17:47:06 +0200 Subject: [PATCH] Refactor pdf plugin code --- _config/eleventy-plugin-pdf.js | 76 +++++++++++++++++----------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/_config/eleventy-plugin-pdf.js b/_config/eleventy-plugin-pdf.js index e525a34..646b8ba 100644 --- a/_config/eleventy-plugin-pdf.js +++ b/_config/eleventy-plugin-pdf.js @@ -1,47 +1,49 @@ import PuppeteerHTMLPDF from "puppeteer-html-pdf"; -import { nanoid } from 'nanoid' +import { nanoid } from 'nanoid'; import fs from 'fs'; -let config = {} -export default function (eleventyConfig, pluginOptions = {}) { - config = Object.assign({ - // Plugin defaults - tempDir: "dist", // Directory where the temporary files are created - debug: false, // Keep the generated temporary html file for debugging - format: "A4", // Default pdf format - margin: { // PDF file margins - top: "1cm", - right: "1.25cm", - bottom: "1cm", - left: "1.25cm", - } - }, pluginOptions); - eleventyConfig.addTransform("toPDF", transformPDF); +const pluginDefaults = { + // Plugin defaults + tempDir: "dist", // Directory where the temporary files are created + debug: false, // Keep the generated temporary html file for debugging + format: "A4", // Default pdf format + margin: { // PDF file margins + top: "1cm", + right: "1.25cm", + bottom: "1cm", + left: "1.25cm", + } }; -const transformPDF = async function(content) { +export default function (eleventyConfig, options = {}) { + const pluginConfig = Object.assign(pluginDefaults, options); + + eleventyConfig.addTransform("toPDF", async (content) => { if (this.outputPath && this.outputPath.toLowerCase().endsWith('.pdf')) { - let tempFileName = config.debug ? this.page.fileSlug : nanoid(); - let htmlFile = `./${config.tempDir}/${tempFileName}.html`; - let pdfFile = `./${config.tempDir}/${tempFileName}.pdf`; - - fs.writeFileSync(htmlFile, content); + let tempFileName = pluginConfig.debug ? this.page.fileSlug : nanoid(); + let htmlFile = `./${pluginConfig.tempDir}/${tempFileName}.html`; + let pdfFile = `./${pluginConfig.tempDir}/${tempFileName}.pdf`; + + fs.writeFileSync(htmlFile, content); - const htmlPDF = new PuppeteerHTMLPDF(); - const options = { - format: config.format, - margin: config.margin, - path: pdfFile, // you can pass path to save the file - }; - htmlPDF.setOptions(options); + const htmlPDF = new PuppeteerHTMLPDF(); + const pdfOptions = { + format: pluginConfig.format, + margin: pluginConfig.margin, + path: pdfFile, + }; + htmlPDF.setOptions(pdfOptions); - const pdfContent = await htmlPDF.readFile(htmlFile, "utf8"); - await htmlPDF.create(pdfContent); - let contents = fs.readFileSync(pdfFile, 'binary'); - - if (!config.debug) { - fs.unlinkSync(htmlFile); fs.unlinkSync(pdfFile); - } - return Buffer.from(contents,'binary'); + const pdfContent = await htmlPDF.readFile(htmlFile, "utf8"); + await htmlPDF.create(pdfContent); + let contents = fs.readFileSync(pdfFile, 'binary'); + + if (!pluginConfig.debug) { + fs.unlinkSync(htmlFile); + fs.unlinkSync(pdfFile); + } + + return Buffer.from(contents,'binary'); } else return content; + }); }; \ No newline at end of file