Convert HTML to Image in Java – HTML to JPG, PNG, BMP, GIF, and TIFF
Converting HTML to images helps you create visual representations of web content for documentation, reports, presentations, or emailing. For example, using HTML to image conversion, you can quickly get images of HTML-based documents like reports, webpages, or charts for preview or thumbnails.
Aspose.HTML for Java library allows you to convert HTML documents to JPG, PNG, GIF, TIFF and BMP file formats with full control over a wide range of conversion parameters. Any HTML to Image conversion can be made with a few required steps:
- Load an existing HTML file from a local file system, URL, stream, or create HTML from scratch.
- Create an ImageSaveOptions object. Here you may set an image format (JPG, PNG, BMP, GIF or TIFF) and customize the rendering process to get the desired result.
- Use one of the
convertHTML()
methods of the Converter class and pass the required parameters to it.
In this article, you will find information on how to convert an HTML to image file formats and how to use ImageSaveOptions. Let’s look at HTML to image conversion scenarios in Java examples!
HTML to JPG in a few lines of Java code
You can convert HTML to Image in your Java application literally with a single line of code! The following Java code shows HTML to JPG conversion:
1// Invoke the ConvertHTML method to convert the HTML code to image.
2Converter.convertHTML(
3 "<h1>Convert HTML to JPG!</h1>",
4 new ImageSaveOptions(ImageFormat.Jpeg),
5 $o("convert-with-single-line.jpg")
6);
In the example, we use the convertHTML(content, baseUri, options, outputPath)
method of the
Converter class that takes four parameters: string with HTML code to be converted, the base folder for the input HTML file, an instance of the ImageSaveOptions class that defines options for the output image, such as image format, and the output file path where the converted image will be saved.
Convert HTML to JPG
The JPG format is commonly used for images because it supports a high compression ratio, which allows you to use smaller files without significant loss in image quality. This makes it ideal for storing images online or sharing them via email or other digital media. To convert HTML to JPG, you should follow a few steps:
- Load an HTML file or create HTML from scratch using the HTMLDocument class.
- Create a new
ImageSaveOptions object with
ImageFormat.Jpeg
.ImageFormat.Png
will be used as default image format. - Use the
convertHTML(document, options, outputPath)
method of the Converter class to save HTML as a JPG image.
The following Java code snippet shows how to convert HTML to JPG using Aspose.HTML:
1// Prepare an HTML code and save it to the file.
2String code = "<span>Hello</span> <span>World!!</span>";
3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("jpg.html"))) {
4 fileWriter.write(code);
5}
6
7// Initialize an HTML document from the file
8HTMLDocument document = new HTMLDocument($o("jpg.html"));
9
10// Create an instance of the ImageSaveOptions class
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
12
13// Convert HTML to JPG
14Converter.convertHTML(document, options, $o("jpg-output.jpg"));
You can download the complete examples and data files from GitHub.
Convert HTML to PNG
PNG is a popular image file format that supports lossless image compression, making it widely used. HTML files can be converted to PNG images for various purposes, such as adding a web page to a PowerPoint presentation, inserting it on a blog, or sending it via email. In the following Java example, we’ll walk through the step-by-step instructions for converting HTML to PNG with default save options:
- Load an HTML file or create HTML from scratch using one of HTMLDocument() constructors of the HTMLDocument class.
- Use the ImageSaveOptions() constructor to create a new
ImageSaveOptions object. The
options
object can be customized to specify different settings for the conversion process. - Call the
convertHTML(document, options, outputPath)
method of Converter class to save HTML as a PNG image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to theconvertHTML()
method as parameters.
1// Prepare an HTML code and save it to the file.
2String code = "<span>Hello</span> <span>World!!</span>";
3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("png.html"))) {
4 fileWriter.write(code);
5}
6
7// Initialize an HTML document from the file
8HTMLDocument document = new HTMLDocument($o("png.html"));
9
10// Initialize ImageSaveOptions
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
12
13// Convert HTML to PNG
14Converter.convertHTML(document, options, $o("png-output.png"));
Convert HTML to BMP
The following example shows how to convert HTML to BMP using Aspose.HTML for Java.
- Load an HTML file using HTMLDocument class.
- Create a new
ImageSaveOptions object with
ImageFormat.Bmp
. By default, the Format property is PNG. - Use the
convertHTML(document, options, outputPath)
method of Converter class to save HTML as a BMP image. The method requires passing the HTMLDocument, ImageSaveOptions, and output file path as parameters.
1// Prepare an HTML code and save it to the file.
2String code = "<span>Hello</span> <span>World!!</span>";
3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("bmp.html"))) {
4 fileWriter.write(code);
5}
6
7// Initialize an HTML document from the file
8HTMLDocument document = new HTMLDocument($o("bmp.html"));
9
10// Initialize ImageSaveOptions
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
12
13// Convert HTML to BMP
14Converter.convertHTML(document, options, $o("bmp-output.bmp"));
Convert HTML to GIF
The following Java code snippet shows how to convert HTML to GIF using Aspose.HTML for Java.
- Load an HTML file using HTMLDocument class.
- Create a new ImageSaveOptions object with GIF ImageFormat.
- Use the
convertHTML()
method of the Converter class to save HTML as a GIF image.
1// Prepare an HTML code and save it to the file.
2String code = "<span>Hello</span> <span>World!!</span>";
3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("gif.html"))) {
4 fileWriter.write(code);
5}
6
7// Initialize an HTML document from the file
8HTMLDocument document = new HTMLDocument($o("gif.html"));
9
10// Initialize ImageSaveOptions
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
12
13// Convert HTML to GIF
14Converter.convertHTML(document, options, $o("gif-output.gif"));
Convert HTML to TIFF
The following Java code snippet shows how to convert HTML to TIFF using Aspose.HTML for Java.
- Load an HTML file using HTMLDocument class.
- Create a new ImageSaveOptions object with TIFF ImageFormat.
- Use the
convertHTML()
method of Converter class to save HTML as a TIFF image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to theconvertHTML()
method as parameters.
1// Prepare an HTML code and save it to the file.
2String code = "<span>Hello</span> <span>World!!</span>";
3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("tiff.html"))) {
4 fileWriter.write(code);
5}
6
7// Initialize an HTML document from the file
8HTMLDocument document = new HTMLDocument($o("tiff.html"));
9
10// Initialize ImageSaveOptions
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
12
13// Convert HTML to TIFF
14Converter.convertHTML(document, options, $o("tiff-output.bmp"));
Save Options – ImageSaveOptions Class
Aspose.HTML for Java allows converting HTML to Images using default or custom save options. ImageSaveOptions allows you to customize the rendering process. You can specify the image format, page size, margins, compression level, media type, etc.
Method | Description |
---|---|
setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW . |
getCss | Gets a CssOptions object which is used for configuration of CSS properties processing. |
setFormat(value) | Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png . |
setBackgroundColor(value) | Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()) . |
setPageSetup(value) | Gets a page setup object is used for configuration output page-set. |
setHorizontalResolution(value) | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
setSmoothingMode(value) | Sets the rendering quality for this image. |
getText() | Gets a TextOptions object which is used for configuration of text rendering. |
The following Java example shows how to use ImageSaveOptions
and create the output image with custom resolutions, page-size, and background color:
1// Prepare HTML code and save it to a file
2String code = "<h1> Image SaveOptions </h1> " +
3 "<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of " +
4 "conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>";
5
6FileHelper.writeAllText($o("save-options.html"), code);
7
8// Initialize an HTML Document from the HTML file
9HTMLDocument document = new HTMLDocument($o("save-options.html"));
10
11// Initialize an ImageSaveOptions object and customize save options
12ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
13options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
14options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
15options.setBackgroundColor(Color.getAntiqueWhite());
16options.getPageSetup().setAnyPage(new Page(new Size(500, 250), new Margin(40, 40, 20, 20)));
17
18// Convert HTML to JPG
19Converter.convertHTML(document, options, $o("save-options-output.jpg"));
To learn more about ImageOptions please read Fine-Tuning Converters article.
Conclusion
Converting HTML to images is a robust solution for creating visual representations of web content for documentation, presentations, previews, thumbnails, and more.
Aspose.HTML for Java simplifies this process by allowing you to convert HTML to several image formats, including JPG, PNG, BMP, GIF, and TIFF. Using the ImageSaveOptions
class, you can customize key aspects of the conversion process, such as image resolution, format, background color, compression, etc.
The Java examples in this article demonstrate the ease and flexibility of using Aspose.HTML for Java to convert HTML to images with minimal code. From quick one-line conversions to more complex customization scenarios, the Java library provides all the tools to efficiently convert HTML to images.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online HTML to Image Converter that converts HTML to Images with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!