Generating a PDF file in Node JS is one of the most common requirements for most web applications these days. Whether you are developing an invoicing system, creating reports, or generating downloadable certificates, the ability to generate PDF in NodeJS brings your app more functionality and professional.
Consider an e-commerce site that must automatically send a customer an invoice upon a purchase. Rather than using third-party services, you can create PDF file in your Node JS application. It saves time, store data locally, and you have complete control over design and it’s content.
In this guide, we’ll explore how to create PDF files in Node JS using a popular package called PDFKit. You’ll learn how to set up your project, install dependencies, and generate different types of PDF documents.
Setting Up Node JS App and Installing Required Packages
Before generating a PDF file, you need a Node JS environment ready. Let’s create new Node.js application and try to generate different types of PDFs. Create a new folder for your project and initialize it with npm:
mkdir node-pdf-example
cd node-pdf-example
npm init -yOnce project is created, you need to install Express and PDFKit, which you will use to create and serve PDF files in node application:
npm install express pdfkitOnce installed, you can start writing js code to generate your PDF documents.
Creating a Basic PDF Document in Node JS
Let’s begin with a simple example that creates a basic PDF file in Node JS using PDFKit. Here, you will just add some text details to PDF file and allow user to download it. Create server.js file into root directory and modify as below:
const express = require('express')
const PDFDocument = require('pdfkit')
const fs = require('fs')
const app = express()
app.get('/basic-pdf', (req, res) => {
const doc = new PDFDocument()
res.setHeader('Content-Type', 'application/pdf')
res.setHeader('Content-Disposition', 'attachment; filename=basic.pdf')
doc.pipe(res)
doc.fontSize(20).text('Hello, this is a simple PDF document created in Node JS.')
doc.end()
})
app.listen(3000)Next, open your browser and enter below URL into it.
http://localhost:3000/basic-pdfOn visiting above URL, It will automatically starts download of PDF file and you can see added content into that PDF document.
Generating a Styled PDF File In Node.js
Above example is focused on generating PDF file in Node.js. In real-world application, Generated PDF document needs to be more formatted and with some styles like colors, format, font-size and many more things. The PDFkit package provides way to implement designs to PDF files. Let’s take another example to generate more stylish PDF files in node.js:
const express = require('express')
const PDFDocument = require('pdfkit')
const fs = require('fs')
const app = express()
app.get('/styled-pdf', (req, res) => {
const doc = new PDFDocument()
doc.pipe(fs.createWriteStream('styled.pdf'))
doc.fontSize(25).fillColor('blue').text('Styled PDF Document', { align: 'center' })
doc.moveDown()
doc.fontSize(14).fillColor('black').text('This PDF includes custom styles, colors, and formatting.')
doc.text('You can use PDFKit to build branded templates for invoices, reports, or certificates.')
doc.end()
res.send('Styled PDF created successfully!')
})
app.listen(3000)When you visit this URL it will generate PDF file but instead of downloading it will store it into file into our Node.js application directory.

Adding Images to a PDF File in Node JS
PDF files often include logos or images, especially for branding or visual reports or invoices. Let’s see another example which demonstrate how to create a PDF file with images in Node JS.
const express = require('express')
const PDFDocument = require('pdfkit')
const fs = require('fs')
const app = express()
app.get('/image-pdf', (req, res) => {
const doc = new PDFDocument()
doc.pipe(fs.createWriteStream('image.pdf'))
doc.image('logo.png', { fit: [150, 150], align: 'center' })
doc.moveDown()
doc.fontSize(16).text('PDF with Images in Node JS', { align: 'center' })
doc.moveDown()
doc.text('This example shows how to include images inside your PDF document using PDFKit.')
doc.end()
res.send('Image PDF created successfully!')
})
app.listen(3000)You can use this method for creating PDF files for product catalogs, certificates, or reports where images add context and value to the document.
Conclusion
It is easy and effective to create a PDF file in Node JS if you are using the appropriate tools. PDFKit can be used with ease to generate PDF documents in NodeJS for applications such as invoices, receipts, reports, or contracts.
By making NodeJS PDF generation a part of your app, you minimize reliance on external tools and maintain maximum customization. Experiment with layouts, images, and fonts to generate rich-looking PDFs that enhance the quality of your app’s output.

