Generating PDF Documents on the Fly Using Nodejs and Kue
Wall Script
Wall Script
Wednesday, April 03, 2019

Generating PDF Documents on the Fly Using Nodejs and Kue

PDF documents are a very common form of hypermedia documents. Whether it’s books, bills, or invoices we consume PDF documents on a daily basis. In this article, we are going to learn how to generate PDF documents using Node.js and Worker queue called Kue. PDF generation is used in products such as E-commerce based applications, airline tickets, hotel booking, etc. We can use the same principle which I am going to show you below and apply it to implement the feature.

Generating PDF Documents on the Fly Using Nodejs and Kue




Developer
Shahid Shaikh
Shahid Shaikh
Engineer. Blogger. Author.
Drone Enthusiast

Prerequisites
You need to have the following software installed:
  • Node.js latest stable version.
  • Redis

You can install Node.js from the official site.

To install Redis in the Ubuntu 18.04, use these commands.
$sudo apt-get update
$sudo apt-get install redis-server
```
To install Redis in the Mac, please read the following article.
Getting Started with Redis, Chatting Application

Start Redis
redis-3.2.8$ src/redis-server

To install Redis on Windows 10, visit this official Redis labs installation guide and follow the steps.

About Kue
Kue is a job queue running on top of Redis. We can use a queue to delegate the tasks to multiple workers and save the processing time. High computing tasks such as video rendering, document generations, heavy mathematical computations are done using a queue system.

I am using Kue to demonstrate how you can use this software to design and scale your system in an efficient way.

Creating Node Project
Create a new folder and switch to it using Terminal or command prompt.



Create a new Node project using the following command.


$npm init --y

Let’s install the dependencies. We will need kue and pdfkit dependency.


$npm install --save kue pdfkit

Awesome. Let’s code our app.

I am going to create two files naming job.js and worker.js.

In Job.js, I will write code to read the file or database and create a Kue job.

In Worker.js, I will write the code to listen to the Kue topic and generate a PDF document when the request comes.

For the sake of simplicity, here are a few entries that I am going to use to generate the PDF document.

invoice.js
module.exports = {
content: [{
'index': 1,
'text': 'Hello Shahid, You are charged $100 this month for services.'
},{
'index': 2,
'text': 'Hello Jason, You are charged $50 this month for services.'
},{
'index': 3,
'text': 'Hello Mark, You are charged $76 this month for services.'
}]
}

Here is our Job.js code.

const kue = require('kue');
const invoice = require('./invoice');
const queue = kue.createQueue();

function startJob() {
let invoiceData = invoice.content;
invoiceData.forEach((singleInvoice) => {
// push data in queue
let job = queue.create('invoice', {
title: `Generate invoice ${singleInvoice.index}`,
template: singleInvoice.text,
}).delay(5000).priority('high').save((err) => {
if(!err) {
console.log('Job added...'+job.id);
}
});
});
}
startJob();

In this code, we are reading the file where information about PDF documents are stored and for each entry, we are creating a new job in the queue.

Here is our Worker.js code.

const kue = require("kue");
const queue = kue.createQueue();
const pdfKit = require('pdfkit');
const fs = require('fs');

function startProcess() {
// listen to the queue
// start processing email
queue.process('invoice', (job,done) => {
// on each request generate the pdf
generatePdfInvoice(job.data, done);
});
}

function generatePdfInvoice(data, done) {
let doc = new pdfKit;
doc.pipe(fs.createWriteStream(`${__dirname}/invoice/${data.title}.pdf`));
doc.fontSize(14).text(data.template, 100, 100);
doc.end();
done();
}

startProcess();
kue.app.listen(4000);

In this code, we are listening to the Kue topic i.e invoice and on each request, we are generating the pdf document. The title of the PDF document should be unique hence we are using the index field to maintain the unique title convention.
Running the app
To run the app, first, run the job.js using the following command.

$node job.js

You should see a similar output like this in the terminal.

Generating PDF Documents on the Fly Using Nodejs and Kue

Now run the worker.js, using the following command.

$node worker.js

In order to verify whether the Job is been added to the queue or not, visit localhost:4000 to see the queue.

Generating PDF Documents on the Fly Using Nodejs and Kue

Now to check the PDF files, open invoice folder and you should see fresh PDF files.

Next steps
You can use a data store such as MySQL, MongoDB to fetch real-world data and generate PDF’s on the fly. You can also use Amazon S3 to store your PDF’s instead of storing it on your server.

Conclusion
We have learned how to use Node and queue system such as Kue to design and build systems to generate PDF’s documents on the fly.
web notification

2 comments:

mailxengine Youtueb channel
Make in India
X