How to convert a multi-page document into an array of PNG Thumbnails in Node.JS

Thumbnails are an excellent way to improve the quality of your app’s user experience. However, setting up automatic generation of thumbnails for individual pages of documents can be quite a process. It will require converting between formats multiple times, as well as splitting document pages and determining these formats to begin with. In short, it’s a slog. Instead of delving into that whole mess, let’s take advantage of an API that I already have setup for just this sort of task.

Installation will require this dependency reference to be added to your package.json file.

"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"

Now on to our function call:

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 = {'pages': 56, // Number | Optional; Specify how many pages of the document will be converted to thumbnails. Default is 1 page.'maxWidth': 56, // Number | Optional; Maximum width of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128.'maxHeight': 56, // Number | Optional; Maximum height of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128.'extension': "extension_example" // String | Optional; Specify the file extension of the inputFile. This will improve the response time in most cases. Also allows unsupported files without extensions to still return a corresponding generic icon.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentAutodetectToThumbnailsAdvanced(inputFile, opts, callback);

To customize our results, we can specify our desired image dimensions (aspect ratio will be preserved), the number of pages to be made into thumbnails, and our file extension if the file does not have one (not necessary, but it will speed up the process of identification). And now if we run it, we will receive an object output that contains an array of thumbnails corresponding to each document page. Easy.

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