How to split a PDF file into separate page PDF files in Node.JS

With this one simple trick, you will be able to turn one PDF file into multiple separate PDFs, each with a single page from the original.

Let’s start with setup of our client.

npm install cloudmersive-convert-api-client --save

Now we can use our client to instantiate an API object, from which we will be able to call splitDocumentPdfByPage.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.SplitDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var opts = {'returnDocumentContents': true // Boolean | Set to true to directly return all of the document contents in the DocumentContents field; set to false to return contents as temporary URLs (more efficient for large operations).  Default is false.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.splitDocumentPdfByPage(inputFile, opts, callback);

And now if you just enter your PDF’s file path, we are good to go. After a few seconds, you will have an array of PDFs, each with a single page from your document. You can also opt to have your response give you download URLs for each page instead using returnDocumentContents.

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store