How to convert a PDF to PNG array in Javascript

While a simple task to perform manually, converting between PDF and PNG arrays can be painful to set up in Javascript. Don’t worry, this won’t be that sort of tutorial. Instead, we will be going the easy route — an API.

So begin with importing our client, which can be done using this script tag in your HTML file:

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-document-convert-client.js"></script>

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

Done! The API will send back URLs that correspond to a PNG for each page of the original file. Cloudmersive offers many other useful format conversion functions like this one, as well as a wide range of editing tools.

Image for post
Image for post

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