Create PDF documents using Node.js (PDFKit)

Introduction

Creating PDF files is a very common requirement in many software applications. In this post we will see how to easily create PDF documents using Nodejs.

So instead of building all PDF files related functionality ourselves, there is node package called PDFKit, that abstracts all this complexity and give us a nice API to use in our NodeJS application.

Along with PDFKit, I’ll be also use another node package faker.js which will help us to generate some data which we can use for pdf document contents.

You can find more information about both these node packages from following URLs:

  • faker.js – generate massive amounts of fake data in the browser and node.js.
  • PDFKit – a JavaScript PDF generation library for Node and the browser.

Project Setup

I am assuming that you have basic knowledge of Node.js and if you are new to this, you can check few of my earlier posts for some information about nodejs in general and its dependency management.

However, here in this post, we are not going to do any thing advance. Just very basic understanding of Nodejs will be enough.

I’ve setup a github repository for the source code and here we have a very basic initial structure for a typical node application. We have an empty server.js file as well as shown below:

We will add more files as we move forward.

Install Packages

Lets install faker and pdfkit packages via npm as shown below:

npm install faker
npm install pdfkit

This will also update package.json file and we are now ready to create our first PDF File. Let’s do that next.

Basic PDF File Creation

All the code is available from the git-hub repo mentioned above. As you can see that we imported faker and pdfkit modules we talked about earlier. We also imported fs module, which will help us with file operations.

The rest of the code is very self-explanatory. It is using faker library to generate some data and using this data as content for pdfDocument. In this case, it is creating various text-lines, paragraphs, setting some alignment and at the end calling doc.end() method to finish to document:

we can now execute this code using terminal as shown in picture. This will generate a pdf document output.pdf. Following is the pdf file generated after code execution.

PDF File Output

More Examples

Beside simple text, paragraphs, we can also use images, graphics, links etc inside the pdf document. There many other examples available online, you can try to see what fits your needs. For example, the following example is based on code from PDFKit git-hub page:

When we execute this code, we get a new pdf file example.pdf and if we open it, then we can see that it has multiple pages and those contains images, vector graphic, HTML Links etc.

Web Server Example

Integrating PDFKit with web-server is also straight forward. In the following example I’ve created a new file webserver.js as shown below:

I am using built-in http module. However, setup is very similar if you use express.js instead.

As you can see that code is very basic and self explanatory. We used the same PDF example code from earlier section, however this time pipe it http response object. Now, We can run the sever and when we visit the root URL using a web browse, we can see the generated PDF document. Following picture shows that PDF file is returned to the user:

Now, in these basic examples, we are hard-coding the data for PDF files. However, you can instead get data from database and other places and then process it to generate PDF files etc.

Summary

In this post, we learned about PDFKit node package, which help us to create PDF documents easily. We saw few examples and possible use cases.

Now, I did not talked about faker.js package much. It is also very popular node package to help you generate test data easily. You can see some example code in the file dataHelper.js which is already available in the code repository.

You can download the source-code from this git repository.

Let me know if you have some comments or questions. Till next time, Happy Coding.


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading