How to Convert RTF to DOCX in Node.JS

Rich Text Format (RTF) is prone to a great many problems, from file corruption to incompatibility troubles. Generally speaking, DOCX is a superior format, and so a conversion is often the best course of action to pursue. For this to work, however, you will need to waste a lot of time coding around the quirky nature of both formats. This rabbit hole (or more likely, quagmire) can easily be avoided if you follow this quick tutorial on how to use an API to solve the problem instead.

Let’s add a dependency reference for our API client to our package.json file.

"dependencies": {
"cloudmersive-convert-api-client": "^2.4.8"
}

Now we may call convertDocumentRtfToDocx, like so:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();var inputFile = "/path/to/file"; // 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);

And you’re done!

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store