We're sorry AsposeCloud doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.cloud

Mail Merge Word Document and Convert output to PDF in Node

I am trying to do a mail merge, but I need the resulting output to be a PDF. So I am trying to take the result of the merge, and pass it to the convert function, but I am running into the following error:

body: WordsApiErrorResponse {
    error: ApiError {
      message: 'Unexpected end of Stream, the content may have already been read by another component. '
    }
}

Here is the code:

const wordsApi = new WordsApi(clientId, secret);

let requestTemplate = fs.createReadStream("TestExecuteTemplate.docx");
let requestData = fs.createReadStream("TestExecuteTemplateData.json");

const mailMergeRequest = new ExecuteMailMergeOnlineRequest({
    template: requestTemplate,
    data: requestData,
    withRegions: true
});

wordsApi.executeMailMergeOnline(mailMergeRequest)
.then((mailMergeRequestResult) => {
    console.log("merge complete");
    const convertRequest = new ConvertDocumentRequest({
        document: Readable.from(mailMergeRequestResult.body),
        format: "pdf"
    });
    wordsApi.convertDocument(convertRequest)
    .then((convertRequestResult) => {
        console.log("convert complete");
        fs.writeFile('output.pdf', convertRequestResult.body, function (err) {
            if (err) return console.log(err);
            console.log('file saved');
        });
    })
    .catch((err) => {
        console.log(err);
    });

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

What should I be doing differently?

@danobri

Please amend the convertRequest code as following, it will fix the issue.

 const convertRequest = new ConvertDocumentRequest({
        //document: Readable.from(mailMergeRequestResult.body),
	document: mailMergeRequestResult.body,
	format: "pdf"
    });

That doesn’t work, as it’s a type mismatch - body is a Buffer, and document is a Readable.

image.png (67.7 KB)

The file conversion does complete successfully without Readable.from(), but it causes the type mismatch shown in the screenshot. So maybe there is something not quite right about the typings file?

@danobri

Thanks for your feedback. We have logged an internal ticket WORDSCLOUD-1747 for more investigation. We will share the results with you asap.