[Node.js] How to merge two or multiple PDF documents into one PDF with Aspose.PDF REST API

FYI: I’m fairly new at working with Aspose products.

I’m running into an issue trying to perform a merge operation on a list of PDF documents in a Node.js application. The API call is simply:

pdfApi.putMergeDocuments('destfile.pdf', { list: ['file1.pdf', 'file2.pdf']}, null, 'folder')

I’ve verified that the issue isn’t with the cloud storage, API authentication, or files themselves. I’ve also tried with and without specifying folders.

The API responds with a generic code=400 error.

As an alternative, I’m using this to test the API with:

await pdfApi.postAppendDocument('destfile.pdf', 'file1.pdf', 0, 1, null, null)
await pdfApi.postAppendDocument('destfile.pdf', 'file2.pdf', 0, 1, null, null)

This works as long as the files are at the root directory of the storage location. If I try to perform the append operation on a file inside a folder, it fails with the same generic code=400 error response.

So my first question is, am I doing something wrong with trying to use the merge function? Or is this an issue with the API?
And my second question, is there a proper convention / method for specifying folder directories in a storage location when performing these operations? Or something else I’m not aware of?

@evoauto

We are sorry for the confusion. Please note when a folder parameter is not defined explicitly for input/output file, then you can pass the complete path in the file parameter. For example, in case of putMergeDocuments, you can notice we have only provided folder parameter for the resulting document in the API reference. So API call would be:

pdfApi.putMergeDocuments('destfile.pdf', { list: ['folder/file1.pdf', 'folder/file2.pdf']}, null, 'folder')

Similarly, for Append API, you can set the folder name separately as parameter for original document, but need to pass the full path of appending document. Hopefully it answers your question.

pdfApi.postAppendDocument('destfile.pdf', 'folder/file1.pdf', 0, 1, null, 'folder')

@tilal.ahmad

This worked like a charm. Thank you for the support.

My errors occurred because I wasn’t referencing the folder paths correctly. I also noticed that the list needs to have more than one PDF file to perform the operation (which makes sense).

1 Like