How to convert a Word DOCX to a PNG Array in Node.JS
Having the ability to convert document files into arrays of PNG images can certainly be useful. So how can we go about doing this without eating up our whole afternoon in the process? Let’s make it easy on ourselves. An API tailor-made for this situation can be applied, saving us all of the struggle that would normally come with this endeavor.
This API is going to need its client, so let’s start by installing that.
"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"
}
And now we can call the function that we need, which I have set up as an example below here.
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 callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentDocxToPng(inputFile, callback);
And now any DOCX file that you input will be returned by the API as an array of PNG images, easy as that. There are also related functions within this client that cover a range of other formats, including PDF, PPTX, and XLSX.