Convert HTML to JPG, PNG, TIFF, or BMP Image in Java

HTML to Image Java

HTML is frequently used to display data and information over web pages, web applications, and different platforms. You may need to convert HTML to an image like JPG, PNG, TIFF, BMP, etc. You can perform the HTML to image conversion programmatically using Aspose.HTML for Java in your Java-based applications. Moreover, the API takes care of rendering HTML to image and you do not need to worry about underlying details of the file formats. Simply use the API calls and the HTML file will be rendered nicely. Let us explore Java HTML to image conversion in detail:

Java HTML to Image Converter – API Installation

Aspose.HTML for Java API supports editing, manipulating as well as converting HTML files using Java language. It can easily be configured by downloading from New Releases or via Aspose Repository with the following installation configurations:

Repository

 <repositories>
     <repository>
         <id>snapshots</id>
         <name>repo</name>
         <url>http://repository.aspose.com/repo/</url>
     </repository>
</repositories>

Dependency

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>20.12</version>
        <classifier>jdk16</classifier>
    </dependency>
</dependencies>

Convert HTML to JPG Image in Java

You can easily convert HTML files to image with few lines of code. Let us learn HTML to JPG image conversion with the steps below:

  1. Load input HTML file
  2. Initialize ImageSaveOptions
  3. Convert HTML to output JPG image

The code snippet below shows how to convert HTML to JPG image using Java code:

Convert HTML to PNG Image using Java

PNG image format is another popular image type which is often preferred because of some image features. For example, a PNG image supports transparency in the picture. You can render a HTML file to PNG image with these steps:

  1. Load input HTML file with HTMLDocument class
  2. Specify ImageFormat as PNG
  3. Save output PNG image

The following code explains how to convert HTML to PNG image:

Converting HTML to TIFF Image in Java

TIFF image file format is popular because of wide support in almost all system environments. You can easily convert HTML to TIFF images with few simple steps:

  1. Initialize HTMLDocument to load input HTML
  2. Specify ImageFormat.Tiff for the output format
  3. Save output TIFF image

The code below elaborates how to convert HTML to TIFF image in Java language:

Convert HTML to BMP Image using Java

Exploring several other methods and properties for HTML to Image conversion, let us explore setting the page size, background color, etc. for the output file. You can easily convert HTML to BMP image with these additional options, using the following steps:

  1. Load input HTML file
  2. Specify size and background color for the output image
  3. Save output TIFF image

The code below demonstrates how to convert HTML file to TIFF image using Java:

Conclusion

We have learned how to convert or render HTML files to different image formats including JPG, PNG, TIFF, BMP, etc using Java. You can further explore the efficiency and features of the API by checking through the examples project. It showcases a lot of features for working with HTML files. In addition, you can always reach out to us at Free Support Forum to discuss any of your requirements or concerns.

See Also

C# Convert HTML to JPG, PNG, BMP and GIF Image with .NET