How to Split A PDF File in Node.js

Introduction

PDF file stands for Portable Document Format. Adobe created a file format independent of operating systems, hardware, or application software to display documents with text formatting and graphics incorporated. PDF files contain text, images, forms, interactive buttons, hyperlinks, embedded typefaces, and other content. They are widely used for document sharing since they maintain their page formatting across a range of hardware and applications. PDFs are often used for forms, eBooks, manuals, and other products where the formatting and layout need to be maintained.

In this article, we are going to use IronPDF, PDF lib which helps to split PDF NodeJs documents into multiple PDF files within an output folder. It can done with a few lines of code. IronPDF is a powerful PDF library that allows us to create, open, read, and split PDF files easily.

What is Node?

JavaScript code can be run outside of a web browser using the cross-platform, open-source Node.js JavaScript runtime environment. With server-side JavaScript execution enabled, programmers may construct scalable, fast, and efficient network applications. Node.js uses an event-driven, non-blocking I/O paradigm, which makes it perfect for creating real-time applications that handle several concurrent connections.

There are various applications that are commonly built with Node.js, including web servers, APIs, streaming apps, real-time chat apps, Internet of Things (IoT) devices, and more. All things considered, Node.js is becoming more and more popular due to its efficiency, speed, and back-end and front-end compatibility with JavaScript, offering a single language for full-stack development. To know more about Node.js we can refer here.

How to split PDF in Node.js

  1. To Split PDFs, download the Node.js package.
  2. Install IronPDF Node.js library.
  3. Create a new PDF or import an existing one.
  4. Use the Duplicate function in the IronPDF Node.js library.
  5. Pass the Page number as a parameter to duplicate the function.
  6. Use save as a function to save the individual pages into a PDF file.

IronPDF for Node.js

IronPDF was primarily a .NET library designed to function within the .NET framework, allowing developers to work with PDF documents using C# or VB.NET, as of my last knowledge update in January 2022. Nevertheless, IronPDF did not have a native or direct version created just for Node.js.

If anything has changed since my last update and IronPDF has grown to include support or bindings for Node.js, this probably indicates that IronPDF for Node.js now offers features for generating, modifying, and processing PDF documents in Node.js applications.

Features of IronPDF

  • HTML to PDF Generation: The capacity to create PDF documents from HTML material.
  • Text and image manipulation refers to the addition, modification, or deletion of text, shapes, images, and other items from PDF files.
  • PDF document modification includes combining, extracting pages from PDF files, splitting PDF files, and, encrypting and decrypting them.
  • Form handling includes filling out forms, obtaining form data, and utilizing PDF forms programmatically.
  • Using digital signatures, encryption, and password protection for PDF documents is known as PDF security.
  • Metadata handling is the process of retrieving and manipulating PDF files.

If IronPDF has extended its product line to include a Node.js version, this might offer developers creating Node.js apps a method to incorporate IronPDF's features for PDF manipulation. Developers who would rather work with a library that provides features similar to those of IronPDF in the .NET environment could find this useful.

For the most up-to-date and accurate information about IronPDF's features, compatibility, and support for Node.js, always consult the official documentation, release notes, or updates from the IronPDF team. Software libraries may have changed or expanded since my last knowledge update. To know more about the IronPDF, refer here.

Package Requirement

  • The IDE is Visual Studio Code.
  • Node.js
  • Installing packages requires package management, which you can use using Yarn or npm.

Install IronPDF package Node

Open Terminal or the Command Prompt: Launch the terminal or command prompt. You can access it in several ways depending on your OS:

  • Windows: Command Prompt or PowerShell
  • Mac OS X Terminal
  • Linux's Terminal

Assemble the package: Use the package name and the npm install command to install a package. For example, run the following command to install the package @ironsoftware/ironpdf:

 npm i @ironsoftware/ironpdf

To install the actual package, replace @ironsoftware/ironpdf with the name of the package you wish to install.

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

Split PDF using IronPDF

It merely takes a few lines of code to split PDF documents into multiple files. Check out our sample to incorporate it into your project. Dividing a single PDF document into several documents is a fairly simple process when using IronPDF. Every document has just one page.

const { PdfDocument } = require("@ironsoftware/ironpdf");
const pdfprocess = async () => {
  console.log("Start split Process");
  // // Load the existing PDF document
  const pdf = await PdfDocument.fromFile("Demo.pdf");
  var pagecount = await pdf.getPageCount();
  for (var i = 0; i < pagecount; i++) {
    var spdf = await pdf.duplicate(i);
    spdf.saveAs("split_" + i + ".pdf");
  }
  console.log("End Split Process");
};
pdfprocess();
JAVASCRIPT

The code sample above demonstrates how to use IronPDF's PdfDocument.fromFile method to open an existing PDF file. By passing a document password to the fromFile method as a second argument, you can also open a password-protected file. The code is quite basic and easy to use to split a PDF file. Using the duplicate method, we may easily copy multiple pages from the original document, as demonstrated above.

By providing the index number as a parameter to the duplicate method, the PDF file is divided. Each page is then saved in a different file. A PDF can also be divided into a range of pages by duplication. For that, we are using the function on getPageCount which will allow us to get the page count of the PDF input file path. Then by using the duplicate method to split the PDF file into multiple PDF files. We can execute the code from the below command line.

How to Split A PDF File in Node.js: Figure 2 - NPM

We came to know that first, it will load the input PDF file and then split PDF files into a separate file.

How to Split A PDF File in Node.js: Figure 3 - PDF Files

To know more about the IronPDF Node.JS library code refer here.

Conclusion

Strong security protections are provided by the IronPDF library to reduce threats and guarantee data safety. It is not restricted to any certain browser and works with all common ones. The library provides many licensing options, including a free developer license and additional development licenses that can be purchased, to meet the varying needs of developers.

Upgrade options are included in the $749 Lite bundle, which also comes with a permanent license, one year of software maintenance, and a thirty-day money-back guarantee. During the watermarked trial period, users can assess the product in real-world application scenarios. For additional information regarding IronPDF's price, licensing, and trial version, kindly click the provided link. To know other products offered by Iron Software refer here.