Initial commit.

This commit is contained in:
TheThomaas 2025-06-08 19:05:19 +02:00
commit 453011285c
6 changed files with 1329 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
docs/*
node_modules
.env
.DS_Store

33
README.md Normal file
View file

@ -0,0 +1,33 @@
# Eleventy Plugin PDF
A plugin for [Eleventy](https://www.11ty.dev/) that generates a pdf file from a template
## Installation
```
npm install --save-dev eleventy-plugin-pdf
```
Open up your Eleventy config file (probably `eleventy.config.js`) and use `addPlugin`:
```js
import eleventyPluginPDF from "eleventy-plugin-pdf";
export default function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginPDF);
};
```
## Options
The `options` object can contain:
| Name | Description | Default |
|---|---|---|
| tempDir | Directory where the temporary files are created | "dist" |
| debug | Keep the generated temporary html file for debugging | false |
| format | Default pdf format | "A4" |
| margin.top | PDF top margin | "1cm" |
| margin.right | PDF right margin | "1.25cm" |
| margin.bottom | PDF bottom margin | "1cm" |
| margin.left | PDF left margin | "1.25cm" |

5
eleventy.config.js Normal file
View file

@ -0,0 +1,5 @@
import generatePdf from "./src/generate-pdf.js";
export default function (eleventyConfig, options) {
return generatePdf(eleventyConfig, options);
};

1209
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

29
package.json Normal file
View file

@ -0,0 +1,29 @@
{
"name": "eleventy-plugin-pdf",
"version": "1.0.0",
"description": "A plugin for 11ty that generates a pdf file from a template",
"main": "eleventy.config.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://codeberg.org/TheThomaas/eleventy-plugin-pdf.git"
},
"keywords": [
"11ty",
"eleventy",
"eleventy-plugin"
],
"author": "TheThomaas",
"license": "ISC",
"homepage": "https://codeberg.org/TheThomaas/eleventy-plugin-pdf#readme",
"bugs": {
"url": "https://codeberg.org/TheThomaas/eleventy-plugin-pdf/issues"
},
"devDependencies": {
"nanoid": "^5.1.5",
"puppeteer-html-pdf": "^4.0.8"
}
}

48
src/generate-pdf.js Normal file
View file

@ -0,0 +1,48 @@
import PuppeteerHTMLPDF from "puppeteer-html-pdf";
import { nanoid } from 'nanoid';
import fs from 'fs';
const pluginDefaults = {
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, outputPath) => {
if (outputPath && outputPath.toLowerCase().endsWith('.pdf')) {
let tempFileName = 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;
});
};