How to Compress A PDF File in Node.js

Introduction

Portable Document Format is referred to as PDF. Adobe developed a file format that displays files—text formatting and graphics included—in a way that isn't dependent on operating systems, hardware, or application software. Text, pictures such as JPG, forms, interactive buttons, hyperlinks, embedded typefaces, and other elements can all be found in PDF files or pages. Because they retain their formatting on a variety of devices and software, they are frequently used for document sharing and compression. Forms, eBooks, manuals, and other publications where maintaining the formatting and layout are popular use PDFs.

In this post, we'll utilize IronPDF, a compress PDF library tool that can compress PDF Node.js in different levels. A few lines of code will be enough. We can simply create, open, read and compress PDF files with IronPDF, a robust PDF library. To know more about data compression level, refer here.

What is Node.js?

A cross-platform, open-source JavaScript runtime environment called Node.js allows JavaScript code to be executed outside of a web browser. It enables programmers to create scalable, quick, and effective network applications by enabling server-side JavaScript execution. Because Node.js is lightweight and ideal for developing real-time applications that manage numerous concurrent connections, it employs an event-driven, non-blocking I/O paradigm.

Web servers, APIs, streaming apps, real-time chat apps, Internet of Things (IoT) devices, and more are among the many applications that are frequently built with Node.js. All things considered, Node.js has become more and more well-liked because of its effectiveness, speed, and compatibility with JavaScript for both front-end and back-end development, providing a single language for full-stack development.

How to compress PDF in Node.js

  1. To compress PDFs, download the Node.js package.
  2. Create a new PDF or import an existing one.
  3. To minimize PDF picture sizes, use the CompressImages technique.
  4. To reduce the PDF's tree structure, apply the CompressStructTree function.
  5. The compressed PDF file should be exported.

IronPDF for Node.js

The main purpose of the .NET library IronPDF is to create, edit, and handle PDF documents inside of .NET applications. IronPDF may not have direct, native support for Node.js, and it is primarily meant to be used with .NET frameworks like C# or VB.NET as of my most recent knowledge update in January 2022.

Features of IronPDF

  • Feature-Rich PDF Operations: IronPDF might offer a full range of features for producing, editing, and modifying PDF documents if it supports Node.js. Features like converting HTML files into PDFs, incorporating text, photos, tables, forms, watermarks, encryption, and more may be included.
  • .NET Compatibility: IronPDF's compatibility with Node.js may be advantageous to developers who are experienced with .NET technologies or who have an established.NET-based infrastructure, as it enables them to take advantage of their expertise and make use of a preferred PDF library in a Node.js environment.
  • Quality and Performance: IronPDF may give dependable and excellent PDF creation and manipulation capabilities by bringing the performance and quality features of .NET-based PDF generation to Node.js applications.
  • Support and Documentation: If IronPDF has added Node.js support, it may offer thorough documentation, sample code, and other resources to help with integration, log and use in Node.js apps.

It's crucial to remember that the benefits would mostly depend on the particular needs of your project as well as IronPDF's capability, compatibility, and ease of interaction with Node.js. For the most up-to-date and accurate information regarding Node.js compatibility and integration, always make sure to study the most recent documentation, official sources, or updates from IronPDF. To know more about the IronPDF refer here.

Package Requirement

  • Visual Studio Code- IDE
  • Node.js
  • A package manager is necessary for installing packages; you can use npm or Yarn for this.

Install IronPDF package Node

Access the Command Prompt or Terminal: Get your cmd or terminal open. Depending on your operating system, you can access it:

  • Windows: PowerShell or Command Prompt
  • Terminal on MacOS
  • Terminal on Linux

Set up the package: To install a package, use the npm install command and the package name. To install a package called @ironsoftware/ironpdf, for instance, enter the following command:

 npm i @ironsoftware/ironpdf

The name of the real package you want to install should be substituted for @ironsoftware/ironpdf.

How to Compress A PDF File in Node.js: Figure 1 - Install IronPDF

Compress PDF using IronPDF

PDF files can be effectively compressed with IronPDF. Keeping the images in the PDF file as small as possible is a crucial strategy. The process of optimization can be initiated by using the compressSize method on the PDF object.

const{PdfDocument}  = require('@ironsoftware/ironpdf');
const pdfprocess=async () => {
    console.log('Start compression Process')
    // Load the existing PDF document from the directory
    const pdf = await PdfDocument.fromFile("Demo.pdf");
    // Compress images with quality parameter (1-100)
    await pdf.compressSize(60);
    // Save the compressed PDF
    await pdf.saveAs("Result_60.pdf");
    // Compress images and scale down based on visible size in the PDF document
    await pdf.compressSize(90, true);
    // Save the scaled and compressed PDF
    await pdf.saveAs("Result_90.pdf");
    console.log('End compression Process')
}
pdfprocess();
JAVASCRIPT

In the above code, First, we are importing the IronPDF library. With the help of the method fromFile which is available in the PdfDocument class, we are loading the existing PDF file. Then we hold them into an object variable which allow us to perform various type of operations from the object variable. We are using another method called compressSize which allows us to pass two parameter image quality and image scale which will optimize PDFs. Then we can use the saveAs method to save the PDF file into the location.

How to Compress A PDF File in Node.js: Figure 2

Result:

How to Compress A PDF File in Node.js: Figure 3

The quality setting is critical when resizing JPEG images. The least amount of loss occurs when the quality level is 100%, but 1% results in very low-quality output. Images with a quality rating of 90% or greater are typically considered high-quality, 80%–90% as medium-quality, and 70%–80% as low-quality. While reducing the quality below 70% could lead to lower-quality photographs, it could also significantly reduce the total file size of the PDF publication.

To find the perfect file size to image quality ratio that suits your particular requirements, it is advisable to experiment with different quality settings. The degree of quality loss will ultimately depend on the sort of input image, with some images likely to lose clarity more noticeably than others. To learn more about this, click this link.

Conclusion

To minimize risks and ensure data protection, the IronPDF library provides strong security features. It is compatible with any popular browser and isn't limited to any particular one. To satisfy the diverse needs of developers, the library offers multiple licensing alternatives, such as a complimentary developer license and supplementary development licenses that can be acquired.

A perpetual license, upgrade options, a year of software maintenance, and a 30-day money-back guarantee are included in the $749 Lite bundle. Users can evaluate the product in practical application settings for the watermarked trial period. Please visit the provided link to learn more about IronPDF's costs, licensing, and trial version. To know other products offered by Iron Software refer here.