Convert Excel to PDF in Node.js throwing HTTP 500 error

I’m using asposecellscloud@21.8.0. The code is very easy:

import { CellsApi, CellsWorkbook_PutConvertWorkbookRequest } from 'asposecellscloud';
import { readFile, writeFile } from 'fs/promises'

const cellsApi = new CellsApi(clientId, clientSecret, 'v3.0');

const xlsRequest = new CellsWorkbook_PutConvertWorkbookRequest({
    file: await readFile(xlsxToConvert),
    format: 'pdf'
});
const xlsRes = await cellsApi.cellsWorkbookPutConvertWorkbook(xlsRequest);

when I run this example with valid credentials, I’m getting HTTP 500.
in the HTTPS trace I’m getting this error:

{
"RequestId":"53094f6e-65c1-4df5-885f-babfb0425cd6",
"Error":{
  "Code":"internalError",
  "Message":"Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')",
  "Description":"Operation Failed. Internal error.",
  "DateTime":"2021-09-29T16:50:42.310799Z"
  }
}

tried the same API method via API tester and via Postman - works fine.

What can be the issue with the nodejs SDK?

@ScottHeide,

We checked your provided code. Please try to use the fs.reateReadStream method.

We attached our test code.

import { expect } from “chai”;

import “mocha”;

const { CellsApi, CellsWorkbook_PutConvertWorkbookRequest } = require(“asposecellscloud”);

var fs = require(‘fs’);

var path = require(‘path’);

var assert = require(‘assert’);

const clientId = process.env.CellsCloudTestClientId;

const clientSecret = process.env.CellsCloudTestClientSecret;

const ApiURL = process.env.CellsCloudTestApiBaseUrl;

const cellsApi = new CellsApi(clientId, clientSecret,“v3.0”,ApiURL);

const filename = “Book1.xlsx”

const localPath = “TestData/”;

describe(‘cellsWorkbookPutConvertWorkbook’, function() {

it(‘should call cellsWorkbookPutConvertWorkbook successfully’, function() {

var req = new CellsWorkbook_PutConvertWorkbookRequest({

  file : fs.createReadStream(localPath  + filename),

  format : "pdf",

});

return cellsApi.cellsWorkbookPutConvertWorkbook(req)

  .then((result) => {         

    console.log(result);

  });

});

});

Thanks, @wangtao, it works (at least the error has gone).
However, the resulting PDF is empty.

Attaching the input file (XSLS in zip archive) and resulting PDF

The same file converted OK if I run the request via Postman.
So looks like it has something to do with the nodejs SDK itself or the way how the response is stored to the file.
I tried writeFile from fs/promises and writeFileSync from fs - the same result (empty file).

output (1).zip (3.3 KB)
output (1).pdf (18.2 KB)

@ScottHeide,
Please try the following codes.

return cellsApi.cellsWorkbookPutConvertWorkbook(req)
.then((result) => {
fs.writeFile(‘test.pdf’,result[‘body’], err => {
if (err) {
console.error(err);
}
}) ;
}) ;
});

@wangtao,

I tried changing my code to what you have suggested - no difference.

However, I grabbed the response coming from the server (using Wireshark) and the response is correct.
I compared it with what comes in the response.body and it looks like binary format is converted to string, which corrupts the data.
See the snapshot.
the left side is what comes in response.body (empty PDF)
the right side is grabbed response (valid PDF)
The line at the bottom shows the difference in bytes: some characters are replaced with 0xFD

It looks like the asposecellscloud has a bug that needs to be fixed.
FYI: checked how docx conversion (ConvertDocumentRequest of asposewordscloud) handles the response - looks like it returns Buffer (which is good).

image.png (205.6 KB)

I decided not to waste time on making SDK functions working, but implemented simple code that uses the API directly. So this thread can be closed.

@ScottHeide,
We thank you for being able to use the NodeJS SDK.
We will continue to solve this problem and optimize the SDK.
We hope you can continue to try the new version of the SDK in the future.

@ScottHeide,

We fixed the bug. Please try 21.9 version.

it(‘should call cellsWorkbookPutConvertWorkbook successfully’, function() {
const filename = “customerOutput.xlsx”;
var req = new CellsWorkbook_PutConvertWorkbookRequest({
file : fs.createReadStream(localPath + filename),
format : “pdf”,
});
return cellsApi.cellsWorkbookPutConvertWorkbook(req)
.then((result) => {
fs.writeFile(‘customerOutput.pdf’,result[‘body’],‘binary’, err => {
if (err) {
console.error(err);
}
}) ;
});
});

Thanks.

However I’ve swithced to raw API, so I’m no longer use these libraries.
But anyway good to know that you’ve fixed it.