npm install dynamic-html-pdf --save
<html>
<head>
Dynamic HTML to PDF
</head>
<body>
<h1>Hi {{users[0].name}}</h1>
<div>
template
</div>
</body>
</html>
Feel free to use handlebar syntax: Handlebar builtin helpers
For example:
<ul>
{{#each users}}
<li>Name: {{this.name}}</li>
<li>Age: {{this.age}}</li>
<li>DOB: {{this.dob}}</li>
{{/each}}
</ul>
var fs = require('fs');
var pdf = require('dynamic-html-pdf');
var html = fs.readFileSync('template.html', 'utf8');
var options = {
format: "A3",
orientation: "portrait",
border: "10mm"
};
var users = [
{
name: 'aaa',
age: 24,
dob: '1/1/1991'
},
{
name: 'bbb',
age: 25,
dob: '1/1/1995'
},
{
name: 'ccc',
age: 24,
dob: '1/1/1994'
}
];
var document = {
type: 'buffer', // 'file' or 'buffer'
template: html,
context: {
users: users
},
path: "./output.pdf" // it is not required if type is buffer
};
pdf.create(document, options)
.then(res => {
console.log(res)
})
.catch(error => {
console.error(error)
});