How to convert any document file into plain text in Node.JS

Sometimes a little compatibility is better than all of the complicated functionality in the world. It’s times like these that you just need to strip out all of the formatting until you’re left with plain text. To do this in Node.JS, we have developed a very simple alternative to coding this all out from scratch: use an API to do it in your place.

Installing our client is the first step, which can be taken care of using npm install:

npm install cloudmersive-convert-api-client --save

Once the installation has completed, we can implement our convertDocumentAutodetectToTxt function, like you see here.

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 opts = {'textFormattingMode': "textFormattingMode_example" // String | Optional; specify how whitespace should be handled when converting the document to text.  Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases.  Default is 'preserveWhitespace'.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentAutodetectToTxt(inputFile, opts, callback);

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