How to convert a PDF to PNG array in Node.js
1 min readMay 20, 2019
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.