Convert Microsoft Word Document to PDF / HTML from streams in Node.js using Aspose.Words REST API

Aspose words cloud node SDK putconvertdocument method, can’t find documentation or examples anywhere.
Can the putconvertdocument method hand file streams as input?
I can save temp files locally and read from my local file system but this is an unnecessary step if putconvertdocument can handle a file stream as input

@rfdesouza

Aspose.Words offer three REST APIs to convert a document to another format:

GET /words/{name}
PUT /words/convert
POST /words/{name}/saveAs

In PUT /words/convert API we pass the input document in the request body whereas in case of GET /words/{name} and POST /words/{name}/saveAs APIs we first upload document to Cloud Storage (using Aspose.Storage Upload API) and pass input document name as an argument to the API.

We will shortly share with you complete Node.js example of PUT /words/convert API.

Okay I have read about your rest APIs, my question is specifically about the SDK for node.

Can the putconvertdocument method handle streams as input?

@rfdesouza

Following is the example of putConvertDocument method of Aspose.Words Node.js SDK:

describe("putConvertDocument function", () => {
            /**
             * Test runner
             * @param value destination fromat
             */
            function runner(value) {
                const request = new PutConvertDocumentRequest({
                    format: value,
                    document: fs.readFileSync(localPath),
                });

                // Act
                return wordsApi.putConvertDocument(request)
                    .then((result) => {
                        // Assert                
                        expect(result.response.statusCode).to.equal(200);
                        expect(result.body.byteLength).to.greaterThan(0);
                    });
            }

            testWithTestCases({
                name: "convert from 'docx' to {value} should return response with code 200",
                values: BaseTest.saveFormatTestCases,
                runner,
            });           
});

If we particularly focus on PutConvertDocumentRequest object you may observe that fs.readFileSync method is taking the file path as input and returns a buffer.

const request = new PutConvertDocumentRequest({
    format: value,
    document: fs.readFileSync(localPath),
});

Following directory contains the Unit Tests (examples) of each Node.js SDK API:

The Unit Tests are grouped by the type of action over the document. Action could be one of two types: we can somehow change document elements, e.g. borders, fields etc. or we can make some document actions e.g. convert, mailmerge and so on.

@rfdesouza

Please note in latest API Version V4.0 PutConvertDocumentRequest method is changed to ConvertDocumentRequest and putConvertDocument method to ConvertDocument. Please find the sample updated code as following and check updated Aspose.Words Cloud SDK for Node.js from GitHub as well.

const { WordsApi, ConvertDocumentRequest } = require("asposewordscloud");
var fs = require('fs');

wordsApi = new WordsApi("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxx");

var request = new ConvertDocumentRequest({
                    format: "pdf",
                    document: fs.createReadStream("02_pages.docx"),
                });
var outputFile = "C:/Temp/ConvertHTMLtoPDF.pdf";
wordsApi.convertDocument(request).then((result) => {    
    console.log(result.response.statusCode);    
    console.log(result.body.byteLength);    
	fs.writeFileSync(outputFile, result.body);
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});