Steps to Create the Application
Step 1: Initialize the Node application
npm init -y
Step 2: Installing Module for setting NodeJS environment also we need to configure the package.json file and PDF module
npm install express pdfkit
For adding new page in the PDF:
doc.addPage()
For saving PDF document in root directory:
doc.pipe(fs.createWriteStream('PDF Name'));
Project Structure:
The updated dependencies in package.json file will look like
"dependencies": {
"fs": "^0.0.1-security",
"pdfkit": "^0.11.0"
}
How to Create PDF Document in Node ?
Creating a PDF document in Node.js can be achieved using various libraries, with pdf-lib
, pdfkit
, and puppeteer
being some of the most popular options. This guide will focus on using pdfkit
to create PDF documents because it is a well-documented and powerful library suitable for a wide range of PDF generation tasks.
Prerequisites:
Syntax:
const PDFDocument = require('pdfkit');
const doc = new PDFDocument;