How to Split PDF Pages into Separate PDFs in Node.js?

TypeError: pdfApi.PostSplitDocument is not a function

Getting above error while using the given api document Split PDF Files|Documentation in nodejs

@skp1992

I have tested to split PDF pages into separate PDFs in Node.js using the latest version of Aspose.PDF Cloud SDK for Node.js and was unable to notice any issue. Please check that you are passing the parameters correctly. However, if you still face any issues then please share your sample code with us for investigation.

How to Split PDF documents in Node.js


const { PdfApi } = require("asposepdfcloud");

pdfApi = new PdfApi("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx","xxxxxxxxxxxxxxxxxx");

var fs = require('fs');


const localFile = "4pages.pdf";
const remoteFile= "4pages.pdf";
const localDataFolder = "C:/Temp";
const remoteFolder = "Temp/Temp";
const format = "pdf";

// Upload File
pdfApi.uploadFile(remoteFolder+"\\"+remoteFile, fs.readFileSync(localDataFolder + "\\" + localFile)).then((result) => {  
		console.log("Uploaded File");

		// Split PDF documents

		//postSplitDocument paramter details
		//@param name Document name
		//@param format Resulting documents format
		//@param from Start page if defined
		//@param to End page if defined
		//@param storage The document storage
		//@param folder The document folder     
 
		pdfApi.postSplitDocument(remoteFile, format, null, null, null, remoteFolder).then((result) => {    
    			console.log(result.body.code);    
			}).catch(function(err) {
			// Deal with an error
			console.log(err);
		});   
                    }).catch(function(err) {
    // Deal with an error
    console.log(err);
});
  1. Could you please update the nodejs doc as I see in for splitting pdf file pdfApi.uploadFile() method is not used in the official document
  2. Your provided code is working fine it would be great help if you add the download code of the splitted pdf.

We are sorry for the inconvenience. We have logged a ticket PDFCLOUD-2620 to fix the issue. Meanwhile, you can check the unit tests from GitHub repository for the sample code.

Please check the sample code to download a file from cloud storage. it will help you to accomplish the task.

const { PdfApi } = require("asposepdfcloud");
//const { MergeDocuments }= require("asposepdfcloud/src/models/mergeDocuments");

pdfApi = new PdfApi("xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxx");
var fs = require('fs');

//Download file
const localPath = "C:/Temp/" + resultName;

pdfApi.downloadFile(resultName).then((result) => {    
    console.log(result.response.statusCode);    
    console.log(result.body.byteLength);    
    fs.writeFileSync(localPath, result.body);
    console.log("File Downloaded");    
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

It was a great support from your side but still I’m facing some issues using the aspose cloud pdf api at my end. For your reference I have shared my code as well as I also attached the log file.

// var dir_path = “C:/Users/Sushilkumar/Documents/Projects/react/upload/uploads/2022-02-08T11-09-53.984Z-2.1 SEO Victory.pdf”

        // var rest = "C:/Users/Sushilkumar/Documents/Projects/react/upload/uploads/"

        // var last = "2022-02-08T11-09-53.984Z-2.1 SEO Victory.pdf"

        const localFile = last;

        const remoteFile= last;

        const localDataFolder = rest;

        const remoteFolder = "Temp/Temp/";

        const format = "pdf";

        // Upload File

        pdfApi.uploadFile(remoteFolder+"/"+remoteFile, fs.readFileSync(localDataFolder + "/" + localFile)).then((result) => {

            console.log("Uploaded File")

           

            pdfApi.postSplitDocument(remoteFile, format, null, null, null, remoteFolder).then((result) => {    

                    // console.log(result);    

                    // console.log(result.body.result.documents);  

                                           

                    result.body.result.documents.forEach(function(splitDoc) {

                        //console.log(splitDoc);

                        //var urlDoc = splitDoc

                        var urlString = splitDoc.href

                        var splittedFilename = urlString.split('/').pop()

                       

                        pdfApi.downloadFile(remoteFolder+"/"+urlString).then((res) => {

                            // console.log(result.response.statusCode)

                            // console.log(result.body.byteLength);

           

                            fs.writeFileSync(rest, res.body)

                            console.log(`File ${splittedFilename} downloaded`);

                        })

                        .catch((err) => {console.log(err);})

                    })

                    .then((res) => {console.log(res)}).catch((err) => {console.log(err);})

                   

                }).catch(function(err) {

                    console.log(err);

            });  

        }).catch(function(err) {

            console.log(err);

        });

log.docx (27.4 KB)

Hello Ahmad,

It was a great support from your side but still I’m facing some issues using the aspose cloud pdf api at my end. For your reference I have shared my code as well as I also attached the log file.

// var dir_path = "C:/Users/Sushilkumar/Documents/Projects/react/upload/uploads/2022-02-08T11-09-53.984Z-2.1 SEO Victory.pdf"
// var rest = "C:/Users/Sushilkumar/Documents/Projects/react/upload/uploads/"
// var last = "2022-02-08T11-09-53.984Z-2.1 SEO Victory.pdf"

const localFile = last;
const remoteFile= last;
const localDataFolder = rest;
const remoteFolder = "Temp/Temp/";
const format = "pdf";

// Upload File
pdfApi.uploadFile(remoteFolder+"/"+remoteFile, fs.readFileSync(localDataFolder + "/" + localFile)).then((result) => {
console.log("Uploaded File")

pdfApi.postSplitDocument(remoteFile, format, null, null, null, remoteFolder).then((result) => {
// console.log(result);
// console.log(result.body.result.documents);

result.body.result.documents.forEach(function(splitDoc) {
//console.log(splitDoc);
//var urlDoc = splitDoc
var urlString = splitDoc.href

var splittedFilename = urlString.split('/').pop()

pdfApi.downloadFile(remoteFolder+"/"+urlString).then((res) => {

// console.log(result.response.statusCode)
// console.log(result.body.byteLength);

fs.writeFileSync(rest, res.body)

console.log(`File ${splittedFilename} downloaded`);
})
.catch((err) => {console.log(err);})
})
.then((res) => {console.log(res)}).catch((err) => {console.log(err);})

}).catch(function(err) {
console.log(err);
});
}).catch(function(err) {
console.log(err);
});

(Attachment debug.log is missing)

Please find the debug file name ‘log.docx’ on below link

log.docx (27.4 KB)

@skp1992

I have updated your code. There were two issues. You were trying to download the file using Href value instead of the file name and there was an extra then/catch block. Please check the following code, it is working fine at my end.

const { PdfApi } = require("asposepdfcloud");
pdfApi = new PdfApi("xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx", "xxxxxxxxxxxxxxxxxx");

var fs = require('fs');


const localFile = "4pages.pdf";
const remoteFile= "4pages.pdf";
const localDataFolder = "C:\\Temp";
const remoteFolder = "Temp/Temp";
const format = "pdf";

 // Upload File

        pdfApi.uploadFile(remoteFolder+"/"+remoteFile, fs.readFileSync(localDataFolder + "/" + localFile)).then((result) => {

            console.log("Uploaded File")

            pdfApi.postSplitDocument(remoteFile, format, null, null, null, remoteFolder).then((result) => {    

                    // console.log(result);    

                    // console.log(result.body.result.documents);  

                                           

                    result.body.result.documents.forEach(function(splitDoc) {

                        //console.log(splitDoc);

                        //var urlDoc = splitDoc

                        var urlString = splitDoc.href

                        var splittedFilename = urlString.split('/').pop()

                       
						**//pdfApi.downloadFile(remoteFolder+"/"+urlString).then((res) => {**
                        pdfApi.downloadFile(remoteFolder+"/"+splittedFilename).then((res) => {

                            // console.log(result.response.statusCode)

                            // console.log(result.body.byteLength);

           

                            fs.writeFileSync(localDataFolder+'/'+splittedFilename, res.body)

                            console.log(`File ${splittedFilename} downloaded`);

                        })

                        .catch((err) => {console.log(err);})

                    })

                   **//.then((res) => {console.log(res)}).catch((err) => {console.log(err);})**

                   

                }).catch(function(err) {

                    console.log(err);

            });  

        }).catch(function(err) {

            console.log(err);

        });

A post was split to a new topic: How to Count PDF Pages in Node.js using Aspose.PDF REST API?