node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป

dynamic-html-pdf

How to Setup: Dynamic handlebars html pdf to create dynamic content html to pdf.

Installation

npm install dynamic-html-pdf --save

Create template.html

<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>

How to use Dynamic HTML to PDF

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)
    });

For detailed Tutorial follow this link