Generate PDF using node.js

Shubham Verma
3 min readMar 29, 2018

--

To create PDF you need to HTML content of that particular, We will generate PDF of same HTML content.

I am using nodejs and I want to generate PDF on specific route ( end point/API ).

So let’s start….

I am using:

var ejs = require(‘ejs’);
var fs = require(‘fs’);
var pdf = require(‘html-pdf’);

Have a look on below code we will use it later. Here is the ejs file where I have written ejs code for dynamic data.

views/indexEJS.ejs

<div>
<div>
<h5><%= appointmentObj.firstName %></h5>
<p><%= appointmentObj.lastName %></p>
</div>
</div>

Image Example:

global/index.js

/**
* Create PDF from HTML and send the URL of PDF.
* You can add
Header/Footer in your PDF also
*/
var pdf = require(‘html-pdf’); // npm install html-pdf — save
global.createPDFFile = function (htmlString, fileName, callback) {
var options = {
format: ‘Letter’,
header: {
“height”: “15mm”,
“contents”: “<img alt=’Clintek logo’
height=’100'
width=’100'
src=’http://52.207.115.173:9191/files/5a6597eb7a67600c64ce52cf/?api_key=25BDD8EC59070421FDDE3C571182F6F12F5AAF99FF821A285884E979F3783B23'>"
},
“timeout”: 600000,
“footer”: {
“height”: “15mm”,
“contents”: {
first: ‘<div>
<span>1</span>
</div>’,
2: ‘<div>
<span>2</span>
</div>’, // Any page number is working. 1-based index
3: ‘<div>
<span>3</span>
</div>’,
4: ‘<div>
<span>4</span>
</div>’,
5: ‘<div>
<span>6</span>
</div>’,
6: ‘<div>
<span>7</span>
</div>’,
default: ‘<div>
<span>Appointment Report</span>
</div>’, // fallback value
last: ‘<div>
<span>Last Page</span>
</div>’,
} }};/**
* It will create PDF of that HTML into given folder.
*/
pdf.create(htmlString, options).toFile(‘./public/pdf/’ + fileName, function (err, data) {
if (err) return console.log(err);
return callback(null, config.get(‘AdminBaseURL’) + ‘:’ +
config.get(‘server.port’) + ‘/pdf/’ + fileName)
});
}

Image Example:

I have an end points where a doctor complete an appointment and after completeion of appointment doctor send the PDF URL to its patient. ( PDF will generate on server and it will store over there ).

Here is the end point:

var route = new Route(‘put’, ‘/cancel-complete-appointment/:id’);

And here is the middleware where I will write the code to generate the PDF.

var global = require(‘../../modules/global/index’);route.use(function (req, res, next) {
var appointMentObj = {
firstName: “Shubham”,
lastName: “Verma”
}
var contents = fs.readFileSync(‘./views/indexEJS.ejs’, ‘utf8’);
/*
* It will convert into HTML with the value according to the
* contents and appointMentObj.
*/
var html = ejs.render(contents, appointMentObj);/*
* Call a user define method to generate PDF.
*/
global.createPDFFile(html, req.params.id + ‘.pdf’, function (err, result) {
if (err) {
console.log(err);
} else {
console.log(“PDF URL ADDED.”);
res.send(result);
}
});
});

Image Example:

Here is the generated pdf ( By me :) )

If you want to create pdf in nodejs from scratch then this link will help you a lot.

Congratulations.. You have created PDF. :)

( Feel free to ask for any doubt )

--

--

Shubham Verma

A full-stack developer, In my free time, I write blogs and play guitar. I am both driven and self-motivated.