How to convert a DOCX document into a Thumbnail PNG Image in Node.JS

Thumbnails can be a great feature, but the process of setting up their creation can be a long and difficult one, especially when documents are involved. I suggest we skip the whole thing and use an API that will convert our DOCX files into customizable thumbnail PNGs. This will save us hours of development time, while still achieving the same results.

To use our API, we are going to need to add this reference for its client to our package.json file.

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

Now call convertDocumentAutodetectToThumbnail, like so:

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 = {'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.convertDocumentAutodetectToThumbnail(inputFile, opts, callback);

Proceed to customize the parameters if you wish, then run the function. Very soon, you will have a thumbnail version of the first page of your DOCX file. This function also works on other document formats, as well as over 100 image types, and can provide generic icons for hundreds of other formats.

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