How to Create PDF Document in Node ?
Last Updated :
07 Jan, 2025
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:
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.
Similar Reads
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 ar
3 min read
How to convert a PDF document to a preview image in PHP? Converting a PDF document into a set of images may not sound that fun, but it can have a few applications. As the content from images cannot be copied that easily, the conversion makes the document strictly 'read-only' and brings an extra layer of protection from plagiarism. The images may also come
6 min read
How to Add HTML and CSS into PDF ? HTML and CSS must now regularly be converted into PDF format during web development. PDFs enable the creation of printable documents, information exchange across several platforms, and preserving a webpage's original layout. There are several ways to convert HTML and CSS to PDF files, from simple on
4 min read
How To Export Data As PDF in Angular? Exporting data in Angular to PDF is a common requirement for applications that handle reports, invoices or downloadable content, in this article, we'll explain how to manually export data from an Angular application to PDF using jsPDF, a popular JavaScript library for creating PDF documents.Prerequi
3 min read
How to add HTML and CSS into PDF File ? HTML and CSS are regularly converted into PDF format during web development. PDFs enable the creation of printable documents, information exchange across several platforms, and preserving a webpage's original layout. Several JavaScript libraries can help us to complete our tasks. Libraries like html
3 min read