How to Convert any Document File to JPG in Node.JS

If you have a massive pile of random document files that need to be unified into a single format, today’s post is going to take you right through the process. In particular, we will be focusing on how to automatically detect incoming document formats and then convert those documents into handy JPGs. As you would no doubt expect, this is actually quite difficult and requires a few thousand lines of code to get right. But don’t worry! We won’t have to deal with all that. Instead we will use an API that can skip us right to the results.

First we install our client:

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

Now we call convertDocumentAutodetectToJpg and pass in our file.

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 = {'quality': 56 // Number | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentAutodetectToJpg(inputFile, opts, callback);

And there you go! Problem solved. Additionally, this same library offers similar means of turning all incoming formats into PDF and PNG as well.

Image for post

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