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.