How to Generate a PDF File in Node.js Darrius Serrant Updated:August 19, 2025 Download IronPDF npm Download Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Gemini Ask Gemini about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Unlock the full potential of PDF generation in your Node.js applications by watching our tutorial, where you'll learn to transform HTML, web pages, and images into dynamic PDFs using IronPDF, and discover how this powerful tool can streamline your development process—subscribe now and start your free trial! more... In this comprehensive tutorial, we delve into generating PDF files in Node.js using the IronPDF library. We begin by installing the necessary packages and setting up the environment. The tutorial covers importing essential modules, including PDF Generator, PDF Document, and IronPDF Global Config, along with the FS module from Node.js. We demonstrate creating PDFs from HTML strings by converting a simple 'Hello World' HTML tag into a PDF. Next, we explore converting a Wikipedia page URL about PDFs into a PDF document, showcasing IronPDF's ability to capture web pages in detail. The tutorial also covers converting images into a single PDF by filtering JPEGs from a directory and using PDF Generator's image-to-PDF functionality. By running the code in the terminal, we successfully generate and review the PDFs, displaying IronPDF's capabilities to convert various sources into PDF documents. This powerful tool significantly enhances PDF generation possibilities in Node.js applications. For more tutorials and to explore IronPDF's features, subscribe to Iron Software's channel and sign up for a trial to experience the software firsthand.# Bash code to install the IronPDF library in your Node.js project.npm install ironpdf # Bash code to install the IronPDF library in your Node.js project.npm install ironpdf SHELL // Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages(); // Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages(); JAVASCRIPT
Updated August 19, 2025 How to Convert a PDF to an Image File in Node.js This video tutorial guides you through converting a PDF file into image files using Iron PDF in JavaScript. Learn how to import PDF documents, extract pages, and save them as BMP images. Discover how to convert specific PDF pages and enhance your application with high-quality images. Read More
Updated September 22, 2025 How to Convert HTML to PDF in Node js without Pupateer | IronPDF Explore a comprehensive guide on using IronPDF to convert HTML to PDF in Node.js. This tutorial provides a step-by-step approach, offering a reliable alternative to Puppeteer for efficient PDF generation. Read More
Updated August 19, 2025 How to Sign A PDF File in Nodejs This tutorial provides a comprehensive guide on signing PDFs using the Iron PDF library in a Node.js environment. It covers installation, configuration, and execution, allowing users to easily add digital signatures to their PDF documents. Read More
Updated August 19, 2025 How to Read PDF Files in Node.js This tutorial guides developers on how to read PDFs using Node.js with the Iron PDF Library. It covers the installation process, importing necessary modules, and demonstrates the asynchronous functions used to extract text and metadata from PDF files. Discover a straightforward approach to handling PDFs in Node.js applications. Read More
Updated August 19, 2025 How to Convert HTML to PDF in Node js without Puppeteer This tutorial explores converting HTML to PDF in Node.js using the Iron PDF library, avoiding Puppeteer. It covers installation, basic conversion from HTML strings, more complex tasks like converting web pages, and working with offline HTML files. The guide highlights Iron PDF's simplicity and versatility. Read More
Updated August 19, 2025 How to Convert HTML to PDF in Nodejs This tutorial guides you through converting HTML to PDF in Node.js using Iron PDF. Learn to install the necessary packages, set up your environment, and create PDFs from HTML strings, files, and web pages. Discover the versatility of Iron PDF for generating professional-quality PDFs. Read More
All your questions are answered to make sure you have all the information you need. (No commitment whatsoever.)