Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article endeavors to delve deep into the nuanced intricacies of IronPDF, guiding readers through every facet from the initial installation process to its practical implementation in real-world scenarios.
PdfDocument.fromHtml
function.PdfDocument.fromUrl
method.imageToPdf
method.saveAs
method.IronPDF stands as a formidable PDF library meticulously crafted for Node.js, endowing developers with the seamless capability to effortlessly generate PDF files. Boasting an extensive array of features, IronPDF proves its versatility, catering to a spectrum of use cases that span from document generation and report creation to content rendering. Among its standout attributes lies the remarkable prowess to seamlessly convert an HTML template and images into PDF files, showcasing the library's adaptability and efficiency in handling diverse content sources.
Before diving into the world of PDF generation, you need to install IronPDF. The process is straightforward, thanks to npm - Node.js's package manager. Open your terminal and run the following command:
npm i @ironsoftware/ironpdf
This command fetches and installs the IronPDF package, making it available for use in your Node.js project.
To install the IronPDF engine that is required for using the IronPDF library, run the following command on the console.
npm install @ironsoftware/ironpdf-engine-windows-x64
Once IronPDF is installed, you need to include the necessary packages in your project using the import statement. This ensures that you have access to the functionalities provided by IronPDF. Consider the following example:
import { PdfDocument } from "@ironsoftware/ironpdf";
This import statement brings in the PdfDocument
class, a fundamental building block for generating PDF files with IronPDF.
Now that IronPDF is set up in your project, let's explore the basic process of generating a PDF file using the PDF creator package. There are many ways to create PDF files using IronPDF, but this article will discuss a few of them.
One of IronPDF's standout features is its ability to convert HTML into PDF files effortlessly. This is particularly useful when you have dynamic content generated on the server and want to present it in a PDF format. Here's a simple example:
import { PdfDocument } from "@ironsoftware/ironpdf";
(async () => {
// Create a PDF from an HTML string
const pdf = await PdfDocument.fromHtml("<h1>Hello Developers This is an Example PDF created with IronPDF</h1>");
// Export the PDF to a file
await pdf.saveAs("output.pdf");
})();
This code snippet utilizes IronPDF, a Node.js library, to dynamically generate a PDF document from a specified HTML string. It begins by importing the PdfDocument
class from the IronPDF package. The subsequent asynchronous function employs the fromHtml
method to create a PDF document, using a simple heading element in this instance. The saveAs
method is then utilized to export the generated PDF to a file named "output.pdf".
The asynchronous nature of the code is managed through the use of the async/await
syntax, ensuring that the PDF data generation and saving processes occur in a non-blocking manner. Overall, this succinct example demonstrates how IronPDF streamlines the creation and manipulation of PDFs in a Node.js environment.
Output PDF generated from an HTML string using IronPDF library
Creating PDF files from a URL using IronPDF in Node.js is a powerful feature that allows you to convert web content into PDF documents programmatically. Here's an example code snippet that demonstrates how to achieve this:
import { PdfDocument } from "@ironsoftware/ironpdf";
(async () => {
// URL of the web page to convert to PDF
const url = "https://google.com";
// Create a PDF doc from the specified URL
const pdf = await PdfDocument.fromUrl(url);
// Save the PDF to a file
await pdf.saveAs("output_from_url.pdf");
})();
In this code:
PdfDocument
class is imported from the IronPDF package.url
is defined with the URL of the web page you want to convert to a PDF.fromUrl
method is then used to create a PDF document from the specified URL. IronPDF fetches the content from the URL and converts it into a PDF document.saveAs
method is employed to save the generated PDF to a file named "output_from_url.pdf."This example showcases the simplicity of using IronPDF to convert web content from a URL into a PDF file, making it a valuable tool for scenarios where dynamic web content needs to be archived or shared in a PDF format.
Output PDF generated from a URL using IronPDF library
In addition to HTML, IronPDF provides functionality to generate PDF files from images. This is particularly useful when dealing with scenarios where images need to be compiled into a single PDF document. Let's explore a simple example:
import { PdfGenerator } from "@ironsoftware/ironpdf";
import fs from 'fs';
(async () => {
// Specify the directory path
const directoryPath = './images';
// Read the contents of the directory
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
// Filter file names to include only .jpg and .jpeg extensions
const jpegFiles = files.filter((file) =>
file.toLowerCase().endsWith('.jpg') || file.toLowerCase().endsWith('.jpeg')
);
// Construct full file paths for the filtered files
const filePaths = jpegFiles.map((file) => `${directoryPath}/${file}`);
// Converts the images to a PDF and save it.
const pdf = PdfGenerator.imageToPdf(filePaths).then(
(returnedPdf) => {
returnedPdf.saveAs("composite.pdf");
});
// Also see PdfDocument.rasterizeToImageFiles() method to flatten a PDF to images or thumbnails
});
})();
This code utilizes IronPDF, a Node.js library, to convert a collection of JPEG images in a specified directory into a single PDF doc. Initially, the code specifies the directory path containing the images. It then reads the contents of the directory using the fs.readdir
function. Subsequently, it filters the file names to include only those with ".jpg" or ".jpeg" extensions.
The full file paths for the filtered images are constructed, and the PdfGenerator.imageToPdf
method is employed to convert these images into a PDF document. The resulting PDF is then saved as "composite.pdf." This script demonstrates the streamlined process offered by IronPDF for converting a batch of images into a consolidated PDF file, providing a practical solution for scenarios where image content needs to be compiled and presented in a single document.
Output PDF generated from Images using IronPDF library
Node.js, coupled with IronPDF, opens up a world of possibilities for dynamic PDF creation. From simple text-based documents to complex reports with dynamic content, IronPDF simplifies the process and enhances the capabilities of your Node.js applications. Whether you're converting HTML to PDF, populating templates, or compiling images into a PDF file, IronPDF provides a versatile and efficient solution for your PDF generation needs.
As you continue to explore and implement these features, you'll find that IronPDF is a valuable tool in your developer toolkit, streamlining the process of creating dynamic and visually appealing PDF documents.
IronPDF for Node.js offers a free trial for their users. To get started with IronPDF visit this documentation page. The code example and complete tutorial of HTML to PDF conversion can be found at the following tutorial page.
9 .NET API products for your office documents