https://github.com/beyluta/html-template-to-pdf
Cross-platform library that converts a html template file into a pdf.
https://github.com/beyluta/html-template-to-pdf
html-template html-templating npm-package pdf pdf-generation pdf-templating
Last synced: about 1 month ago
JSON representation
Cross-platform library that converts a html template file into a pdf.
- Host: GitHub
- URL: https://github.com/beyluta/html-template-to-pdf
- Owner: beyluta
- License: mit
- Created: 2023-02-21T20:16:47.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-20T11:13:57.000Z (about 2 years ago)
- Last Synced: 2025-03-28T02:53:02.548Z (about 2 months ago)
- Topics: html-template, html-templating, npm-package, pdf, pdf-generation, pdf-templating
- Language: JavaScript
- Homepage:
- Size: 35.2 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# html-template-to-pdf
Cross-platform library that converts a html file into a pdf.
Take a look at [`html-pdf-node package`](https://github.com/mrafiqk/html-pdf-node) for additional pdf generation options.
# Installation
```
npm install html-template-to-pdf
```# Usage
Here is an example of how to generate a pdf from `index.html`.
```javascript
const fs = require("fs");
const generatePDF = require("html-template-to-pdf");async function main() {
const arrayBuffer = await generatePDF("index.html");
const fileStream = fs.createWriteStream("output.pdf");
fileStream.write(Buffer.from(arrayBuffer));
fileStream.end();
}main();
```# Templating
### Replacing placeholders
The library allows you to use templating to generate dynamic pdfs. It uses the second argument of the `generatePDF` function. Here is an example of how to use it.
```javascript
const fs = require("fs");
const generatePDF = require("html-template-to-pdf");async function main() {
const arrayBuffer = await generatePDF("index.html", { str: "World!" });
const fileStream = fs.createWriteStream("output.pdf");
fileStream.write(Buffer.from(arrayBuffer));
fileStream.end();
}main();
```The HTML file `index.html` should look like this.
```html
Hello { str }
```The output pdf will look like this:
```
Hello World!
```# Pseudo conditional templating
### Showing or hiding elements
You can also use pseudo conditional templating to hide or show elements. Here is an example of how to use it.
```javascript
const fs = require("fs");
const generatePDF = require("html-template-to-pdf");async function main() {
/*
** When `show` is true, the element will be shown.
** When `show` is false, the element will be hidden.
*/
const arrayBuffer = await generatePDF("index.html", { show: true });
const fileStream = fs.createWriteStream("output.pdf");
fileStream.write(Buffer.from(arrayBuffer));
fileStream.end();
}main();
```The HTML file `index.html` should look like this.
```html
Hello ?{show World! }?
```The output pdf will look like this:
```
Hello World!
```Or if `show` is false, the output pdf will look like this:
```
Hello
```### Negative conditions
Use the prefix `!` in front of the condition to make it negative. In the following example: the element will only be visible if `show` is false
```htmlHello ?{!show World! }?
```# Example
```javascript
const fs = require("fs");
const generatePDF = require("html-template-to-pdf");async function main() {
const arrayBuffer = await generatePDF("index.html", {
employeeName: "John Doe",
salary: "$9000",
show: true,
});
const fileStream = fs.createWriteStream("output.pdf");
fileStream.write(Buffer.from(arrayBuffer));
fileStream.end();
}main();
``````html
?{show
Hello, { employeeName }
?{salary
Your salary is { salary }
}?
}?
```
Would generate:
```
Hello, John Doe
Your salary is $9000
```