How to Convert Files to Thumbnail Image Objects using Node.js

Cloudmersive
2 min readDec 21, 2023

--

Thumbnail images offer small & distinct content previews for would-be document viewers. By incorporating a thumbnail generation API into our application, we can reap the benefits of document thumbnails with minimal code.

Thanks to the complementary, ready-to-run code provided further down the page, we can easily generate lightweight PNG thumbnails for dozens of documents — including everything from Office document formats to PDFs and more than 100 unique image types — in our Node.js applications using a free API. In our request, we can customize the number of pages we want to convert in our document (default is 1), and we can specify the maximum height and width of the output file (default is 128 for both values).

To authorize our API requests, we’ll just need a free-tier API key; this will allow us to make up to 800 thumbnail conversions per month with no additional commitment.

We can begin structuring our API call by installing the SDK. To do that, we can either use the below command:

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

Or we can add the below snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

With installation out of the way, we can now copy the below code into our file and call the function (with our API key, file, and parameter customizations set):

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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);

That’s all there is to it — now we can easily generate PNG thumbnails for free with minimal code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet