Split PDF Documents using REST API in Node.js

Split PDF Documents using REST API in Node.js

You may need to split PDF files into several files programmatically. By splitting PDF documents, you can easily extract and share a specific piece of information or a set of data with the stakeholders. As a Node.js developer, you can split PDF documents into multiple documents on the cloud. In this article, you will learn how to split PDF documents using a REST API in Node.js.

The following topics shall be covered in this article:

PDF Splitter REST API and Node.js SDK

For splitting PDF files, I will be using the Node.js SDK of GroupDocs.Merger Cloud API. It allows you to split, combine, remove and rearrange a single page or a collection of pages from supported document formats of Word, Excel, PowerPoint, Visio drawings, PDF, and HTML.

You can install GroupDocs.Merger Cloud to your Node.js application using the following command in the console:

npm install groupdocs-merger-cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Split PDF Documents into One-Page Documents using REST API in Node.js

You can split PDF files programmatically on the cloud by following the simple steps mentioned below:

Upload the PDF File

Firstly, upload the PDF file to the Cloud using the code example given below:

As a result, the uploaded PDF file will be available in the files section of your dashboard on the cloud.

Split PDF Documents using REST API in Node.js

You can easily split pages of any PDF file into separated PDF documents consisting of one page in a document programmatically by following the steps given below:

  • Create an instance of the DocumentApi.
  • Create an instance of the FileInfo.
  • Then, set path to the input PDF file.
  • Create an instance of the SplitOptions.
  • Then, assign FileInfo to the SplitOptions.
  • Set specific page numbers in a comma separated array to split document.
  • Also, set document split mode to Pages. It allows API to split page numbers given in comma separated array as a separate PDF documents.
  • Create SplitRequest with SplitOptions.
  • Finally, call the DocumentAPI.split() method with SplitRequest and get results.

The following code snippet shows how to split a PDF file using a REST API in Node.js.

Split PDF Files into One-Page Documents using Node.js
Split PDF Files into One-Page Documents using Node.js

Download the Split Files

The above code sample will save the separated files on the cloud. You can download them using the following code sample:

Split PDF Files into MultiPage PDF Documents using Node.js

You can split PDF files into multipage PDF documents programmatically by following the steps given below:

  • Create an instance of the DocumentApi.
  • Create an instance of the FileInfo.
  • Then, set path to the input PDF file.
  • Create an instance of the SplitOptions.
  • Then, assign FileInfo to the SplitOptions.
  • Set page numbers interval from where to split in a comma separated array.
  • Also, set document split mode to Intervals. It allows the API to split document pages based on the page intervals given in a comma separated array.
  • Create SplitRequest with SplitOptions.
  • Finally, call the DocumentAPI.split() method with SplitRequest and get results.

The following code snippet shows how to split a PDF file into multipage PDF documents using a REST API in Node.js.

Split PDF Files into MultiPage PDF Documents using Node.js
Split PDF Files into MultiPage PDF Documents using Node.js

Extract Pages by Page Range using REST API in Node.js

You can extract and save pages from a PDF file by providing a range of page numbers programmatically by following the steps given below:

  • Create an instance of the DocumentApi.
  • Create an instance of the FileInfo.
  • Then, set path to the input PDF file.
  • Create an instance of the SplitOptions.
  • Then, assign FileInfo to the SplitOptions.
  • Set the start page number and the end page number.
  • Also, set document split mode to Pages.
  • Create SplitRequest with SplitOptions.
  • Finally, call the DocumentAPI.split() method with SplitRequest and get results.

The following code snippet shows how to split a PDF file by page numbers range using a REST API in Node.js.

Extract Pages by Page Range using REST API in Node.js
Extract Pages by Page Range using REST API in Node.js

Try Online

Please try the following free online PDF splitter tool, which is developed using the above API. https://products.groupdocs.app/splitter/pdf/

Conclusion

In this article, you have learned how to split PDF documents using a REST API on the cloud. Moreover, you have seen how to split PDF files into multipage PDF documents programmatically. This article also explained how to programmatically upload a PDF file to the cloud and then download the separated files from the Cloud. The API also enables you to reorder or replace document pages, change page orientation, manage document passwords and perform other manipulations easily for any supported file format. Besides, you can learn more about GroupDocs.Merge Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also