Refactor pdf plugin code
This commit is contained in:
parent
9788f158c2
commit
f09135319a
|
|
@ -1,10 +1,8 @@
|
||||||
import PuppeteerHTMLPDF from "puppeteer-html-pdf";
|
import PuppeteerHTMLPDF from "puppeteer-html-pdf";
|
||||||
import { nanoid } from 'nanoid'
|
import { nanoid } from 'nanoid';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
let config = {}
|
const pluginDefaults = {
|
||||||
export default function (eleventyConfig, pluginOptions = {}) {
|
|
||||||
config = Object.assign({
|
|
||||||
// Plugin defaults
|
// Plugin defaults
|
||||||
tempDir: "dist", // Directory where the temporary files are created
|
tempDir: "dist", // Directory where the temporary files are created
|
||||||
debug: false, // Keep the generated temporary html file for debugging
|
debug: false, // Keep the generated temporary html file for debugging
|
||||||
|
|
@ -15,33 +13,37 @@ export default function (eleventyConfig, pluginOptions = {}) {
|
||||||
bottom: "1cm",
|
bottom: "1cm",
|
||||||
left: "1.25cm",
|
left: "1.25cm",
|
||||||
}
|
}
|
||||||
}, pluginOptions);
|
|
||||||
eleventyConfig.addTransform("toPDF", transformPDF);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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')) {
|
if (this.outputPath && this.outputPath.toLowerCase().endsWith('.pdf')) {
|
||||||
let tempFileName = config.debug ? this.page.fileSlug : nanoid();
|
let tempFileName = pluginConfig.debug ? this.page.fileSlug : nanoid();
|
||||||
let htmlFile = `./${config.tempDir}/${tempFileName}.html`;
|
let htmlFile = `./${pluginConfig.tempDir}/${tempFileName}.html`;
|
||||||
let pdfFile = `./${config.tempDir}/${tempFileName}.pdf`;
|
let pdfFile = `./${pluginConfig.tempDir}/${tempFileName}.pdf`;
|
||||||
|
|
||||||
fs.writeFileSync(htmlFile, content);
|
fs.writeFileSync(htmlFile, content);
|
||||||
|
|
||||||
const htmlPDF = new PuppeteerHTMLPDF();
|
const htmlPDF = new PuppeteerHTMLPDF();
|
||||||
const options = {
|
const pdfOptions = {
|
||||||
format: config.format,
|
format: pluginConfig.format,
|
||||||
margin: config.margin,
|
margin: pluginConfig.margin,
|
||||||
path: pdfFile, // you can pass path to save the file
|
path: pdfFile,
|
||||||
};
|
};
|
||||||
htmlPDF.setOptions(options);
|
htmlPDF.setOptions(pdfOptions);
|
||||||
|
|
||||||
const pdfContent = await htmlPDF.readFile(htmlFile, "utf8");
|
const pdfContent = await htmlPDF.readFile(htmlFile, "utf8");
|
||||||
await htmlPDF.create(pdfContent);
|
await htmlPDF.create(pdfContent);
|
||||||
let contents = fs.readFileSync(pdfFile, 'binary');
|
let contents = fs.readFileSync(pdfFile, 'binary');
|
||||||
|
|
||||||
if (!config.debug) {
|
if (!pluginConfig.debug) {
|
||||||
fs.unlinkSync(htmlFile); fs.unlinkSync(pdfFile);
|
fs.unlinkSync(htmlFile);
|
||||||
|
fs.unlinkSync(pdfFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Buffer.from(contents,'binary');
|
return Buffer.from(contents,'binary');
|
||||||
} else return content;
|
} else return content;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
Loading…
Reference in a new issue