How to convert an HTML Document to PNG Array in Node.JS

Our mission today is to implement a system for converting any and all HTML files into arrays of PNG image files using Node.JS. Far from being the challenge it might first suggest, the whole process will be simplified immensely through a simple shortcut that I am about to show you.

Let’s get started. With npm install, our setup is a breeze:

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

Moving to the next step, we are now able to use our function, convertDocumentHtmlToPng.

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

Our implementation is already complete. If you now enter an HTML file as the input, you will be given back a set of URLs corresponding to PNGs for each page of the original file — simple as that. If you need to use external files, for example images, make sure you have absolute URLs for each of these.

Image for post
Image for post

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