Open In App

How to Create PDF Document in Node ?

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating a PDF document in Node can be achieved using various libraries like pdf-lib, pdfkit, and puppeteer. This article 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;

Approach

To create PDF document in Node we will be using the pdfkit library. First, import PDFDocument from pdfkit and fs. Create a write stream with fs.createWriteStream(‘PDF Name’) and pipe the document. Add text, images, SVG transformations, blue text, and a hyperlink. Finalize and close the document to complete it.

Steps to Create PDF Document in Node

Step 1: Initialize the Node application

npm init -y

Step 2: Install pdfkit module

Installing Module for setting NodeJS environment also we need to configure the package.json file and PDF module

npm install express pdfkit

Step 3: For adding new page in the PDF

doc.addPage()

Step 4: For saving PDF document in root directory

doc.pipe(fs.createWriteStream('PDF Name'));

Project Structure:

Screenshot2021

The updated dependencies in package.json file will look like

"dependencies": {
"express": "^4.18.2",
"pdfkit": "^0.13.0"
}

Explanation

  • Initialization:
    • Import ‘PDFDocument’ and ‘fs’.
    • Create a new PDF document and set up a stream to save it.
  • Content Addition:
    • Add text, an image, and a new page with text and SVG path transformations.
  • Annotations and Finalization:
    • Add a page with blue text and a link to GeeksforGeeks.
    • Finalize and close the PDF file.
  • For adding new page in the PDF.
doc.addPage()
  • For saving PDF document in root directory.
doc.pipe(fs.createWriteStream('PDF Name'));

Example: Below is the practical implementation of the create PDF document in Node.js.

Node
// 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.
doc.pipe(fs.createWriteStream('example.pdf'));

// Adding functionality
doc
    .fontSize(27)
    .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'
});

doc
    .addPage()
    .fontSize(15)
    .text('Generating PDF with the help of pdfkit', 100, 100);



// Apply some transforms and render an SVG path with the 
// 'even-odd' fill rule
doc
    .scale(0.6)
    .translate(470, -380)
    .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
    .fill('red', 'even-odd')
    .restore();

// Add some text with annotations
doc
    .addPage()
    .fillColor('blue')
    .text('The link for GeeksforGeeks website', 100, 100)
    .link(100, 100, 160, 27, 'https://www.geeksforgeeks.org/');

// Finalize PDF file
doc.end();

Output: Created PDF file will look like this.



Next Article

Similar Reads