Creating PDF Document with JSPDF in Node JS

When working with Node JS, you often have the requirements to print PDF Documents. There are many npm packages available but unfortunately, many of them are useless. The library that works on the front end and back end is jsPDF.  This library gives you a lot of options to format documents and insert images very easily.

How to use JSPDF in Node JS

First, you need to install jsPDF packages with npm or yarn. Run one of the following commands to install the package in your project.

The above command will install the package in your project.

Printing a Simple Document with jsPDF

Now, let’s use the package to simply print a document with a text line. The following code snippets require the library in the first line and the next couple of lines are used to print the document on your machine.

Changing the Orientation of PDF Document

By default, the options for the document are a4 paper with portrait type. You can change the options by passing an object when initializing the jsPDF. The following example changes the default options and set the page to landscape.

Adding Image to PDF with Node JS

You can add images to the PDF document but before that, you need to convert the image to base64. There are many packages that can do the job of converting an image to a base64 string on npm like node-base64-image. The following code example adds an image to the document and saves it as a PDF file.

You can read more about addImage method on the official docs of jsPDF.


Creating a PDF file with node js is very easy if you are using jsPDF package. You can add text, images, and many more elements in PDFs. The library allows you to specify different options to style the document, and use custom fonts.

Latest articles

Related articles