How to Convert HTML to JPG in Java

This quick tutorial is designed to provide an explanation of how to convert HTML to JPG in Java by providing details for configuring the required environment and then demonstrating the complete process using the sample Java code. For converting HTML to JPG Java code shows how to load the source HTML file, set the configuration for the output JPG image, and finally save it on disk. You can save the output image to other types as well by setting the image format.

Steps to Convert HTML to JPG using Java

  1. Set the project environment to use Aspose.HTML from the Maven repository
  2. Import necessary classes in the program required to convert HTML to JPG
  3. Load the source HTML using HTMLDocument class object to convert it to JPG
  4. Initialize ImageSaveOptions to configure output JPG image
  5. Convert HTML to JPG by calling Converter.convertHTML() function

These steps describe how using Java HTML to JPG conversion can be performed by providing a link to the required resources on the Maven repository and then providing hints to import necessary classes. In the next steps order of operations is described as loading the source HTML file, instantiating ImageSaveOptions class object to configure the output image, and finally saving it on disk.

Code to Convert HTML to JPG in Java

In order to convert HTML to JPG Java uses import directive for importing necessary classes, HTMLDocument class to load source HTML file, ImageSaveOptions class object to configure the output JPG image and Converter.convertHTML() function to convert HTML to JPG. The resolution of the image is set by providing value and units and then used to set the horizontal and vertical resolution. The ImageFormat.Jpeg enumerator is used to set the image type that contains a lot of other image type options also.

In this article, we have learned how to convert HTML to JPG in Java, however if you want to learn the conversion of HTML to text, refer to the article on how to convert HTML to text in Java.

 English