How to Convert HTML to PNG Image Array in Java

2 min readAug 5, 2021


Manually transforming an HTML document into an image file is no easy task — it involves multiple steps and can end up taking hours away from your day. Fortunately, this tutorial is focused on demonstrating how you can use an API in Java to automatically convert HTML to a PNG image array in just a few minutes.

We will begin the Maven installation by adding a reference to the repository in pom.xml:


Then, we can add a reference to the dependency:


Now we can add the imports to the top of the controller and call the function with the following code:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
PdfToPngResult result = apiInstance.convertDocumentHtmlToPng(inputFile);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentHtmlToPng");

Note that if external files are present in the HTML, you should use an absolute URL to the file instead of the simple file.




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