How to convert PDF to Text TXT in Node.JS

PDF format is great and all, but sometimes you really just need some elegantly simple TXT, am I right? If you’re worried that we are about to dive into a steaming pile of format conversion jargon to accomplish this conversion, well, put your worries aside. We are going to handle this whole thing with two simple steps. Let’s take a look.

npm install can be used to handle the installation of our API client:

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

Once that’s finished, it’s time for our function:

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 PDF 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.convertDocumentPdfToTxt(inputFile, opts, callback);

And that’s really all there is to it. Once you’ve called the function, you will be returned the converted TXT file. Do note the optional handling of whitespace.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

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