How to Convert RTF to DOCX in Node.js
Unlike DOCX, RTF files don’t support tables or images. To enable those features, we’ll need to convert our RTF files to DOCX format.
Thankfully, we can easily convert RTF to DOCX using the ready-to-run Node.js code examples further down the page. We can use this code to call a free specialized document conversion API, and we can authorize our request with a free-tier API key (this allows up to 800 API calls per month with no commitments).
Our first step is to install the SDK. Let’s either run the following command:
npm install cloudmersive-convert-api-client --save
Or, alternatively, add the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Last but not least, let’s copy the below code into our file and supply our API key:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentRtfToDocx(inputFile, callback);
That’s all the code we’ll need — now we can make our RTF to DOCX conversions at scale.