Move pdf plugin to separate package
This commit is contained in:
parent
f09135319a
commit
057c7768df
|
|
@ -1,49 +0,0 @@
|
|||
import PuppeteerHTMLPDF from "puppeteer-html-pdf";
|
||||
import { nanoid } from 'nanoid';
|
||||
import fs from 'fs';
|
||||
|
||||
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",
|
||||
}
|
||||
};
|
||||
|
||||
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 = 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 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 (!pluginConfig.debug) {
|
||||
fs.unlinkSync(htmlFile);
|
||||
fs.unlinkSync(pdfFile);
|
||||
}
|
||||
|
||||
return Buffer.from(contents,'binary');
|
||||
} else return content;
|
||||
});
|
||||
};
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { inspect } from "util";
|
||||
import eleventyPluginPDF from "eleventy-plugin-pdf";
|
||||
import pluginIcons from 'eleventy-plugin-icons';
|
||||
import pluginPDF from "./_config/eleventy-plugin-pdf.js";
|
||||
import pluginTokens from "./_config/eleventy-plugin-tokens.js";
|
||||
import shortcodes from "./_config/shortcodes.js";
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ export default async function (eleventyConfig) {
|
|||
|
||||
eleventyConfig.addPlugin(shortcodes);
|
||||
eleventyConfig.addPlugin(pluginTokens, { tokens: "./src/_data/tokens.json", destination: "./src/css/theme.css" });
|
||||
eleventyConfig.addPlugin(pluginPDF);
|
||||
eleventyConfig.addPlugin(eleventyPluginPDF);
|
||||
eleventyConfig.addPlugin(pluginIcons, {
|
||||
sources: [
|
||||
{ name: 'lucide', path: 'node_modules/lucide-static/icons', default: true },
|
||||
|
|
|
|||
7
package-lock.json
generated
7
package-lock.json
generated
|
|
@ -11,6 +11,7 @@
|
|||
"devDependencies": {
|
||||
"@11ty/eleventy": "^3.0.0",
|
||||
"eleventy-plugin-icons": "^4.5.3",
|
||||
"eleventy-plugin-pdf": "^1.0.1",
|
||||
"lucide-static": "^0.503.0",
|
||||
"nanoid": "^5.1.5",
|
||||
"puppeteer-html-pdf": "^4.0.8",
|
||||
|
|
@ -1289,6 +1290,12 @@
|
|||
"node": ">=16.20.2"
|
||||
}
|
||||
},
|
||||
"node_modules/eleventy-plugin-pdf": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/eleventy-plugin-pdf/-/eleventy-plugin-pdf-1.0.1.tgz",
|
||||
"integrity": "sha512-kmbjyhSZnXJktYR8WHYQIATPvbSbC7kjxr22FUIx/khEnXG3aHDFcKOB4HNGpUeajee524AHgkBZZDV8sRmYwg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/emoji-regex": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
"devDependencies": {
|
||||
"@11ty/eleventy": "^3.0.0",
|
||||
"eleventy-plugin-icons": "^4.5.3",
|
||||
"eleventy-plugin-pdf": "^1.0.1",
|
||||
"lucide-static": "^0.503.0",
|
||||
"nanoid": "^5.1.5",
|
||||
"puppeteer-html-pdf": "^4.0.8",
|
||||
|
|
|
|||
Loading…
Reference in a new issue