Visit Original Non-AMP Page

How to Convert HTML to Image in Java - 5 Different Methods

Thanks to all the Available Libraries out there (Like Flying Saucer), That make Converting HTML Documents to images easy.
Let’s explore All the Different Ways How to convert HTML to an image in Java.
There are so many Libraries available inside Java that can be used, and I’ll cover Most of them in this article.

Converting HTML to Image using Flying Saucer Library

Flying Saucer is a pure Java library that we can use for Converting HTML Documents to Images.
Where Flying Saucer Library includes rendering arbitrary XML using CSS for layout and formatting.
Flying Saucer Library can Convert HTML Documents to Swing panels, PDFs, and Images.

Use Flying Saucer in your Java Project

In order to use Flying Saucer in your Java Project, you will need to add it as a Dependency through Maven or Gradle.

implementation group: 'org.xhtmlrenderer', name: 'flying-saucer-core', version: '9.1.22'
<dependency>
  <groupId>org.xhtmlrenderer</groupId>
  <artifactId>flying-saucer-core</artifactId>
  <version>9.1.22</version>
</dependency>

Use Flying Saucer Conversion Logic

After Integrating Flaying Saucer into your Java Project, now you can use Flaying Soucer's API to Render HTML or Convert HTML to an Image.
Just Parse your HTML Element as a Parameter and Flaying Soucer will handle the Rest.

import org.xhtmlrenderer.swing.Java2DRenderer;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class HTMLToImageConverter {
    public static void main(String[] args) throws Exception {
        // Your HTML Here
        String htmlContent = "<html><body><h1>Hello, World!</h1></body></html>";
        
        // Converting HTML to Image
        BufferedImage image = new Java2DRenderer(htmlContent).getImage();
        
        // Saving the Converted Image
        File output = new File("output.png");
        ImageIO.write(image, "png", output);
    }
}

Converting HTML to Image using Selenium WebDriver

You can also use Selenium WebDriver to Convert HTML documents to an Image.
By using Selenium WebDriver you can Capture a Screenshot of your HTML and Save that Screenshot.

Setting Up Selenium WebDriver

To use Selenium WevDriver in Java, you need to Import them inside your Java Project.
After that you need to configure your Web Browser (We are going to use Chrome with Selenium).

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

Using Selenium WebDriver Conversion Logic

After Importing Selenium and Chrome WebDrivers, you need to load your HTML Content in a Browser and Capture the Screenshot and after that, you can Save that Screenshot.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;

public class HTMLToImageConverter {
    public static void main(String[] args) throws Exception {
        // Setup Chome Drivers
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
        // Creating WebDriver instance
        WebDriver driver = new ChromeDriver();
        
        // Load HTML content in the Browser
        driver.get("data:text/html,<html><body><h1>Hello, World!</h1></body></html>");
        
        // This will Capture the Screenshot
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        
        // Storing Captured Images to file
        FileUtils.copyFile(screenshot, new File("output.png"));
        
        // Closing the WebDriver
        driver.quit();
    }
}

Other Available Methods for Converting HTML Content

If you are not able to Convert your HTML Content to an Image by using Flying Saucer and Selenium WebDriver, then you can try other available Libraries like:

GroupDocs.Conversion Library

GroupDocs.Conversion Library acts as a Middleware that empowers your Java Project to convert more than 50 Document Types to Different Image formats.

Graphics2D Custom Rendering Library

Graphics2D classes provide more refined control over geometry, coordinate modifications, color control, and text format. This is the absolute class for rendering 2D shapes, text, and images on the Java platform.

Aspose.Words Library for Java

Aspose.Words (available for Java and Python) is a class library that allows you to do a wide range of document-processing tasks.
Aspose.Words for Java Library also Provides HTML-to-image conversion capabilities.