How to convert PDF to Text TXT in Node.JS

Cloudmersive
2 min readMar 21, 2020

--

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet