How to convert any Document File into PNG Array in Node.JS

Cloudmersive
2 min readApr 10, 2020

PNGs are the great equalizers of file formatting, with virtually universal ability in compatibility, printing, and display. Thus, if we can just convert our document files into PNG arrays, we can make use of this versatility and convenience. If you were to attempt this from scratch, you would no doubt have a bad time with it. The process is long and irritating — I know because I’ve already done it. This is why I packaged my completed solution into an API for you to use. Here’s how.

Let’s start by using npm install to import the Cloudmersive API client.

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

With the client installed, we are now able to call convertDocumentAutodetectToPngArray, the function that we need.

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.convertDocumentAutodetectToPngArray(inputFile, callback);

Now we can enter any document file that we wish and our lovely API will automatically detect the format and then provide a list of URLs for our PNG files, respectively. Done!

--

--

Cloudmersive

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