How to convert a PDF to PNG array in Node.js

The first step for this task is to add the package.json reference to the needed library:

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

Now from here, we simply call the specific convertDocumentPdfToPngArray method:

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

This will return a JSON object with URLs to all of the PNG files for individual pages in the PDF file. We can download one or all of those files easily. That’s it! That is all that is needed to convert the PDF pages into a PNG array.

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