How to Convert DOCX to PNG in Node.js
As useful as DOCX might be for some — specifically, those working in & around the MS Office ecosystem — it’s equally inconvenient to others who lack access to Office applications. An easy way to share a current (un-editable) version of a DOCX document is to convert it to PNG format first, effectively creating a screenshot which can be viewed on any operating system at any time. You can easily make this conversion using the below Node.js code; just copy & paste, include your file, and then authenticate the API service with a free-tier Cloudmersive API key (get one by registering a free account on our website).
Our first step is to install the SDK; to do so, let’s run the below command:
npm install cloudmersive-convert-api-client --save
Next, we need to call the function, including our file path and API key in their respective fields (labeled in the code comments for your convenience):
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 callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentDocxToPng(inputFile, callback);
…and that’s all there is to it! All done.
Note: for mult-page DOCX documents, this conversion will result in a PNG image array with exactly one image per page.