Free Support Forum - aspose.cloud

Convert DOCX to PDF and save result to local drive with Aspose.Words Cloud SDK for Node.js

I am using node.js and the PUT /words/convert API to convert a docx to a pdf, I get the response body, but am trying to write the body to a pdf file on my computer. I cannot open the file because of corruption issues. Most likely something to do with the encoding

@adesai6

Thanks for your inquiry. Please check following Node.js code to save response body to PDF file. Hopefully it will help you to accomplish the task.

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

// Get App Key and App SID from https://dashboard.aspose.cloud/
wordsApi = new WordsApi("xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxx");

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