HTML-PDF example using Mustache.js
Ever wonder if we can have multiple copies of a thing with a little change in every copy. Not a common use but sometimes without this, things can get messy. Say, you want a photo frame with same design but different photograph and name for the person of the photograph.
Why i am pinching this topic here ??
because i had to generate an invoice for a sale order, but the invoice would be different for every order but with same design across all invoices.
So, i created a template for my invoice (invoice-template) and generated multiple invoices by providing different content data to each template.
This is achieved using following libraries -
- Mustache.js — a templating engine to generate html string from a given template and a data object. Mustache is a logic-less template syntax. It can be used for HTML, config files, source code- anything. It works by expanding tags in a template using values provided in a hash or object.
- Html-pdf — a node library to generate pdf from html string. It spawns a phantom-js process and passes the pdf as buffer or as filename.
Necessary files —
- package.json
- server.js
- template file with data array
Step 1 : Create a simple node project
Create a simple node project using npm init command and follow the instructions. After successful creation, package.json is created.

Step 2 : Create a javascript file — server.js
Create an index file which is going to be a server and will handle every request, say it’s server.js. Create a new file server.js and add the required code to create a simpe server.

Add proper dependencies to the file.

Step 3 : create a javascipt file which contains an object with template and template data.
Now the final step is to have a template file which will be rendered as html after data is provided into the template. This template will understand mustache.js syntax and data is provided accordingly.


Finally, above mentioned two objects are returned from this sale Invoice template file to be used in server.js

So, after creating the template file we have to handle a request which will generate the required html content and will be send in response, and the browser requesting the server will be receive the pdf content in response and can show the generated pdf in the window.
Server.js method to handle pdf code.

Run your server.js file using node command, node server.js
Say, if your server is running at port 3232, and the above method is called if you request is for ‘/sale’ url then the pdf returned from the server to browser is opened as the following output -

This covers the html-pdf library example working with mustache.js
Please look for necessary links :
- find the full project here for clear understanding : Github Project
- Mustache.js npm
- Html-pdf npm
There will be many instances where you will need to have pdf generation requirement in the project, key is how you are going to generate pdf content and where. And by where, i mean server side or client side.
PS: Many libraries are available for pdf-generation- you just have to figure out your requirement.
