Convert HTML to Image in Java – PNG, JPG, BMP, GIF, 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 and MemoryStreamProvider parameters. Let’s look at HTML to Image conversion scenarios in Java examples!
HTML to JPG by a single line 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 HTML to JPG
2 com.aspose.html.converters.Converter.convertHTML("<h1>Convert HTML to Image in Java</h1>", ".", new ImageSaveOptions(ImageFormat.Jpeg), Path.combine(getOutputDir(), "convert-with-single-line.jpg"));
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 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 using HTMLDocument class.
- Create a new
ImageSaveOptions object with JPG ImageFormat.
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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "file.html");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "file-output.jpg");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize an ImageSaveOptions instance
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14
15 // Convert HTML to JPG
16 com.aspose.html.converters.Converter.convertHTML(document, options, outputPath);
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 using one of HTMLDocument() constructors of the HTMLDocument class.
- Use 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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "nature.html");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "nature-output.png");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize an ImageSaveOptions object
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15 // Convert HTML to PNG
16 com.aspose.html.converters.Converter.convertHTML(document, options, outputPath);
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 BMP ImageFormat. 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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "bmp.html");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "bmp-output.bmp");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize ImageSaveOptions
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
14
15 // Convert HTML to BMP
16 com.aspose.html.converters.Converter.convertHTML(document, options, outputPath);
Convert HTML to GIF
The following Java code snippet shows how to convert HTML to GIF using Aspose.HTML.
- 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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "spring.html");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "spring-output.gif");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize ImageSaveOptions
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
14
15 // Convert HTML to GIF
16 com.aspose.html.converters.Converter.convertHTML(document, options, outputPath);
Convert HTML to TIFF
The following Java code snippet shows how to convert HTML to TIFF using Aspose.HTML.
- 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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "nature.html");
3
4 // Prepare a path for converted file saving
5 String outputPath = Path.combine(getOutputDir(), "nature-output.tiff");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize ImageSaveOptions
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
14
15 // Convert HTML to TIFF
16 com.aspose.html.converters.Converter.convertHTML(document, options, outputPath);
Save Options
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, CSS 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 a path to a source HTML file
2 String documentPath = Path.combine(getDataDir(), "color.html");
3
4 // Prepare a path for converted file saving
5 String savePath = Path.combine(getOutputDir(), "color-output.jpg");
6
7 // Initialize an HTML document from the file
8 HTMLDocument document = new HTMLDocument(documentPath);
9 try { }
10 finally { if (document != null) document.dispose(); }
11
12 // Initialize an ImageSaveOptions object and customize save options
13 ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14 options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
15 options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
16 com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());
17 options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(500, 500), new Margin(30, 20, 10, 10)));
18
19 // Convert HTML to JPG
20 com.aspose.html.converters.Converter.convertHTML(document, options, savePath);
To learn more about ImageOptions please read Fine-Tuning Converters article.
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!