Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this comprehensive tutorial, we delve into generating PDF files in Node.js using the Iron PDF 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 Iron PDF 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 Iron PDF'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 Iron PDF'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.
Further Reading: How to Generate a PDF File in Node.js
# Bash code to install the Iron PDF library in your Node.js project.
npm install ironpdf
# Bash code to install the Iron PDF library in your Node.js project.
npm install ironpdf
// Import necessary modules from Iron PDF and Node.js
const { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');
const fs = require('fs'); // File system module for handling file operations
// Initialize PDF Generator
const 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 PDFs
generatePdfFromHtml();
generatePdfFromWebPage();
generatePdfFromImages();
// Import necessary modules from Iron PDF and Node.js
const { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');
const fs = require('fs'); // File system module for handling file operations
// Initialize PDF Generator
const 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 PDFs
generatePdfFromHtml();
generatePdfFromWebPage();
generatePdfFromImages();