How to Create and Manipulate PDF Documents in Node.js with 'PDFKit' Module ?
Creating and manipulating PDF documents programmatically is a common requirement in many web applications. Whether it's generating invoices, reports, or complex forms, the ability to create PDFs directly from your server-side code can greatly enhance the functionality of your application. In this article, we will explore how to create and manipulate PDF documents in Node.js using the PDFKit
module.
Introduction to PDFKit
PDFKit
is a JavaScript library that provides an easy-to-use API for generating and modifying PDF documents in Node.js. It allows you to create complex PDF documents from scratch, including text, images, vector graphics, and more. PDFKit
is robust, flexible, and supports a wide range of PDF features.
Installation Steps:
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2: Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 4: Install the necessary packages/libraries in your project using the following commands.
npm install pdfkit
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"pdfkit": "^0.15.0",
}
Approach
After successful installation, we can use this package in our JavaScript application. To use this package, we have to import it first using the require function and we also have to import 'fs' module because we are interacting with the pdf file. Then create a new pdf document using the createWriteStream( ) function by passing the pdf file name in it, then add content in it. You can add text by using fontSize and text methods. To add images we have to use the image method by passing the file name and properties of the image.
Example: Implementation to create and manipulate PDF Documents in NodeJs.
// index.js
// Importing required modules
const PDFDocument = require("pdfkit");
const fs = require("fs");
// Creating a new instance of PDFDocument class
const doc = new PDFDocument();
// Piping the output stream to a file
// named "output.pdf"
doc.pipe(fs.createWriteStream("output.pdf"));
// Setting the fill color to green and
// font size to 30
doc.fillColor("green")
.fontSize(30)
.text("GeeksforGeeks");
// Setting the fill color to black and
// font size to 15
doc.fillColor("black")
.fontSize(15)
.text("It is the best platform for :- ");
// Adding multiple lines of text to
// the document
doc.text("Programming");
doc.text("Data Structures");
doc.text("Web development");
doc.text("Android development");
doc.text("Artificial intelligence, etc.");
// Ending the document
doc.end();
Run the code: After successfully adding the content in the pdf file, we can run the javascript code using the command shown below. This will create a new pdf document having all the content described in the code.
node index.js
Output:
Conclusion
The PDFKit
module is a powerful tool for creating and manipulating PDF documents in Node.js. It provides a wide range of features for adding text, images, shapes, and more to your PDFs. Whether you need to generate simple reports or complex documents, PDFKit
offers the functionality you need to get the job done.