-
Step 1 - install the pdf creator package using the following command
$ npm i html-pdf-lambda --save
--save flag adds package name to package.json file.
-
Step 2 - Add required packages and read HTML template
//Required package const pdf = require("html-pdf-lambda"); const fs = require("fs"); // Read HTML Template const html = fs.readFileSync("template.html", "utf8");
-
Step 3 - Create your HTML Template
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>{{ title }}</title> </head> <body> <h1>User List</h1> <ul> {{#each users}} <li>Name: {{this.name}}</li> <li>Age: {{this.age}}</li> <br /> {{/each}} </ul> </body> </html>
-
Step 4 - Provide HTML and user data
const data = { title: "Hello World!", users: [ { name: "Shyam", age: "26", }, { name: "Navjot", age: "26", }, { name: "Vitthal", age: "26", }, ] }; // The file buffer will be developed;
-
Step 5 - After setting all parameters, just pass document and options to
pdf.create
method.pdf .create(html, data) .then((res) => { console.log(res); }) .catch((error) => { console.error(error); });
Please refer to the following if you want to use conditions in your HTML template:
html-pdf-lambda is MIT licensed.