Create plugin to generate pdf
This commit is contained in:
parent
f5bebc057e
commit
21c1f28994
38
_config/eleventy-plugin-pdf.js
Normal file
38
_config/eleventy-plugin-pdf.js
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import PuppeteerHTMLPDF from "puppeteer-html-pdf";
|
||||||
|
import { nanoid } from 'nanoid'
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
let config = {}
|
||||||
|
export default function (eleventyConfig, pluginOptions) {
|
||||||
|
config = pluginOptions;
|
||||||
|
eleventyConfig.addTransform("toPDF", transformPDF);
|
||||||
|
};
|
||||||
|
|
||||||
|
const transformPDF = async function(content) {
|
||||||
|
if (this.outputPath && this.outputPath.toLowerCase().endsWith('.pdf')) {
|
||||||
|
let htmlFile = `./${config.tempDir || "dist"}/${nanoid()}.html`;
|
||||||
|
let pdfFile = `./${config.tempDir || "dist"}/${nanoid()}.pdf`;
|
||||||
|
|
||||||
|
fs.writeFileSync(htmlFile, content);
|
||||||
|
|
||||||
|
const htmlPDF = new PuppeteerHTMLPDF();
|
||||||
|
const options = {
|
||||||
|
format: config.format || "A4",
|
||||||
|
margin: config.margin || {
|
||||||
|
top: "1cm",
|
||||||
|
right: "1.25cm",
|
||||||
|
bottom: "1cm",
|
||||||
|
left: "1.25cm",
|
||||||
|
},
|
||||||
|
path: pdfFile, // you can pass path to save the file
|
||||||
|
};
|
||||||
|
htmlPDF.setOptions(options);
|
||||||
|
|
||||||
|
const pdfContent = await htmlPDF.readFile(htmlFile, "utf8");
|
||||||
|
await htmlPDF.create(pdfContent);
|
||||||
|
let contents = fs.readFileSync(pdfFile, 'binary');
|
||||||
|
|
||||||
|
fs.unlinkSync(htmlFile); fs.unlinkSync(pdfFile);
|
||||||
|
return Buffer.from(contents,'binary');
|
||||||
|
} else return content;
|
||||||
|
};
|
||||||
|
|
@ -3,6 +3,7 @@ import browserslist from "browserslist";
|
||||||
import * as sass from "sass";
|
import * as sass from "sass";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { bundle, browserslistToTargets, composeVisitors, transform } from "lightningcss" ;
|
import { bundle, browserslistToTargets, composeVisitors, transform } from "lightningcss" ;
|
||||||
|
import eleventyPDF from "./_config/eleventy-plugin-pdf.js";
|
||||||
|
|
||||||
export default async function (eleventyConfig) {
|
export default async function (eleventyConfig) {
|
||||||
eleventyConfig.setUseGitIgnore(false);
|
eleventyConfig.setUseGitIgnore(false);
|
||||||
|
|
@ -29,6 +30,8 @@ export default async function (eleventyConfig) {
|
||||||
.sort((a, b) => a.data.startDate - b.data.startDate).reverse();
|
.sort((a, b) => a.data.startDate - b.data.startDate).reverse();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
eleventyConfig.addPlugin(eleventyPDF);
|
||||||
|
|
||||||
eleventyConfig.addPassthroughCopy('src/favicon.ico')
|
eleventyConfig.addPassthroughCopy('src/favicon.ico')
|
||||||
|
|
||||||
// Recognize CSS as a "template language"
|
// Recognize CSS as a "template language"
|
||||||
|
|
|
||||||
1109
package-lock.json
generated
1109
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -15,6 +15,8 @@
|
||||||
"@11ty/eleventy": "^3.0.0",
|
"@11ty/eleventy": "^3.0.0",
|
||||||
"browserslist": "^4.24.4",
|
"browserslist": "^4.24.4",
|
||||||
"lightningcss": "^1.29.3",
|
"lightningcss": "^1.29.3",
|
||||||
"sass": "^1.87.0"
|
"sass": "^1.87.0",
|
||||||
|
"nanoid": "^5.1.5",
|
||||||
|
"puppeteer-html-pdf": "^4.0.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue