How to Convert HTML to Image in Java

This short tutorial is aimed at explaining how to convert HTML to image in Java by loading an HTML file and then saving it as an image say PNG in this tutorial. You can set a number of parameters of the output image including the resolution that is demonstrated here. For writing HTML to image converter Java language is used here however you may perform this task in many other languages and platforms also.

Steps to Convert HTML to Image in Java

  1. Configure the project to add Aspose.HTML from the Maven repository for HTML to image conversion
  2. Load a newly created or an existing HTML file into the HTMLDocument class object
  3. Instantiate ImageSaveOptions class object and set image type as PNG
  4. Set the resolution of the output image
  5. Convert the HTML file to PNG using the specified save options

To convert HTML to image Java language-based steps are explained here. You may add the necessary libraries into the project and import the required classes used in the sample code. Then following a step-by-step approach load an HTML file, define the output image parameters and finally convert it into the required image type.

Code to Convert HTML to Image in Java

import java.io.FileWriter;
import com.aspose.html.HTMLDocument;
import com.aspose.html.License;
import com.aspose.html.converters.Converter;
import com.aspose.html.drawing.Resolution;
import com.aspose.html.drawing.UnitType;
import com.aspose.html.rendering.image.ImageFormat;
import com.aspose.html.saving.ImageSaveOptions;
public class ConvertHtmlToImageInJava {
public static void main(String[] args) throws Exception {//main function to convert HTML to Image
// Instantiate the license to avoid water mark in the converted image
License licenseHtmlToImage = new License();
licenseHtmlToImage.setLicense("Aspose.html.lic");
// Create an HTML file locally to test the feature
String code = "<html><body><h1>This is heading h1</h1><p>Here is a paragraph enclosed in p tag</p></body></html>";
try (FileWriter fileWriter = new FileWriter("document.html"))
{
fileWriter.write(code);
}
// Load an existing HTML file to convert to image
HTMLDocument document = new HTMLDocument("document.html");
try
{
// Create ImageSaveOptions class object and initialize it with the PNG format
ImageSaveOptions pngImageoptions = new ImageSaveOptions(ImageFormat.Png);
Resolution resolution = new Resolution(300, UnitType.DPI);
pngImageoptions.setHorizontalResolution(resolution);
pngImageoptions.setVerticalResolution(resolution);
// Export HTML to PNG using the Converter.convertHTML() function
Converter.convertHTML(document, pngImageoptions, "output.png");
}
finally
{
if (document != null)
{
document.dispose();
}
}
System.out.println("Done");
}
}

This code demonstrates how using Java generate image from HTML using a few lines of code where first we have created an HTML file locally however it is not necessary and you may load any existing HTML file also into the HTMLDocument class object. In the next step initialize the ImageSaveOptions class object that is required during the conversion and optionally set some parameters like resolution etc. Ultimately the conversion is performed using the Converter.convertHTML() function that takes source HTML file, image save options, and output image file name.

In this topic, we have observed the process to convert HTML to image in Java, however if you want to create a rich HTML file from scratch, refer to the article on how to create HTML file using Java.

 English