Skip to content
Related Articles
Open in App
Not now

Related Articles

How to create PDF document in Node.js ?

Improve Article
Save Article
  • Last Updated : 24 Feb, 2022
Improve Article
Save Article

In this article, we will see how to generate PDF documents in NodeJS.


Step 1: Install PDF module. The PDFkit– It is an Inbuilt Module for generating the PDF documents in the NodeJS in a very simpler and easy way in which we can make graphics, load images and also provide link of the website.

  • For installing it in the project write the following command on the terminal or command prompt.
npm install pdfkit


const PDFDocument = require('pdfkit');
const doc = new PDFDocument;
  • For adding new page in the PDF.
  • For saving PDF document in root directory.
doc.pipe(fs.createWriteStream('PDF Name'));

Step 2: Installing Module for setting NodeJS environment. Also we need to configure the package.json file.

npm install express

Filename: package.json


  "name": "node_func",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "dev": "nodemon app.js"
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fs": "^0.0.1-security",
    "pdfkit": "^0.11.0"

Folder Structure: We can see that the PDF file is created in root directory.

Folder Structure

Example: Here is the JavaScript code that should be written in app.js which is for NodeJS.

Filename: index.js


// Importing modules
import PDFDocument from 'pdfkit'
import fs from 'fs'
// Create a document
const doc = new PDFDocument();
// Saving the pdf file in root directory.
// Adding functionality
  .text('This the article for GeeksforGeeks', 100, 100);
// Adding an image in the pdf.
  doc.image('download3.jpg', {
    fit: [300, 300],
    align: 'center',
    valign: 'center'
  .text('Generating PDF with the help of pdfkit', 100, 100);
// Apply some transforms and render an SVG path with the 
// 'even-odd' fill rule
  .translate(470, -380)
  .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
  .fill('red', 'even-odd')
// Add some text with annotations
  .text('The link for GeeksforGeeks website', 100, 100)
  .link(100, 100, 160, 27, '');
// Finalize PDF file

Output: Created PDF file will look like this.

Created PDF file

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!