How to convert an HTML document file to text (TXT) in Node.JS

Converting an HTML document into TXT might seem pretty basic at first glance, but there is a lot potential pitfalls lurking just beneath that surface. Why deal with the process at all? How about we take the easy way out instead? That’s what I’m here to show you.

Begin by installing our client with this command for npm install.

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

With that done, we will be able to access our API functions. Call convertDocumentHtmlToTxt to continue.

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.convertDocumentHtmlToTxt(inputFile, callback);

And we are already done! Now wasn’t that easier than trying to code it out from scratch? This same API has a lot of other useful functions like this. For example a whole slew of other document conversions.

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