Convert HTML to Image in Java

To convert HTML to an image in Java, load the source with HTMLDocument, create ImageSaveOptions with the required ImageFormat, and call Converter.convertHTML(). Supported output formats include JPG, PNG, BMP, GIF, and TIFF.

HTML-to-image conversion creates a visual representation of web content for documentation, reports, presentations, previews, and thumbnails. Aspose.HTML for Java uses ImageSaveOptions to select the image format and configure settings such as resolution, page size, margins, background color, smoothing, and TIFF compression.

Convert HTML to JPG with One convertHTML() Call

The following example passes an HTML string, base URI, JPEG save options, and output path directly to Converter.convertHTML():

1// Convert HTML to JPG in one line using Java
2
3// Invoke the convertHTML() method to convert HTML code to image
4Converter.convertHTML(
5        "<h1>Convert HTML to JPG!</h1>",
6        ".",
7        new ImageSaveOptions(ImageFormat.Jpeg),
8        "convert-with-single-line.jpg"
9);

The base URI ("." in the example) provides the context for resolving relative resources. ImageSaveOptions(ImageFormat.Jpeg) selects JPG output, which is saved as convert-with-single-line.jpg.

Convert HTML to JPG

JPG uses lossy compression and is useful when smaller image files are more important than transparency or lossless detail.

  1. Create the source HTML and load it into an HTMLDocument.
  2. Create ImageSaveOptions with ImageFormat.Jpeg.
  3. Call Converter.convertHTML(document, options, outputPath) to convert the document to JPG.
 1// Convert HTML to JPG using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello,</span> <span>World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("jpg.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("jpg.html");
11
12// Create an instance of the ImageSaveOptions class
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14
15// Convert HTML to JPG
16Converter.convertHTML(document, options, "jpg-output.jpg");

Download complete examples and data files from GitHub.

Convert HTML to PNG

PNG uses lossless compression and is a suitable choice when text edges, diagrams, or transparency must remain clear.

  1. Create the source HTML and load it into an HTMLDocument.
  2. Create ImageSaveOptions with ImageFormat.Png. PNG is also the default format for the parameterless ImageSaveOptions() constructor.
  3. Call Converter.convertHTML(document, options, outputPath) to convert the document to PNG.
 1// Convert HTML to PNG using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello,</span> <span>World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("png.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("png.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15// Convert HTML to PNG
16Converter.convertHTML(document, options, "png-output.png");

Convert HTML to BMP

The following workflow selects BMP explicitly:

  1. Create the source HTML and load it into an HTMLDocument.
  2. Create ImageSaveOptions with ImageFormat.Bmp.
  3. Call Converter.convertHTML(document, options, outputPath) to convert the document to BMP.
 1// Convert HTML to BMP using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello,</span> <span>World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("bmp.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("bmp.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
14
15// Convert HTML to BMP
16Converter.convertHTML(document, options, "bmp-output.bmp");

Convert HTML to GIF

To select GIF output:

  1. Create the source HTML and load it into an HTMLDocument.
  2. Create ImageSaveOptions with ImageFormat.Gif.
  3. Call Converter.convertHTML(document, options, outputPath) to convert the document to GIF.
 1// Convert HTML to GIF using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello,</span> <span>World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("gif.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("gif.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
14
15// Convert HTML to GIF
16Converter.convertHTML(document, options, "gif-output.gif");

Convert HTML to TIFF

To select TIFF output:

  1. Create the source HTML and load it into an HTMLDocument.
  2. Create ImageSaveOptions with ImageFormat.Tiff.
  3. Pass the document, options, and output path to Converter.convertHTML().
 1// Convert HTML to TIFF using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello,</span> <span>World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("tiff.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("tiff.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
14
15// Convert HTML to TIFF
16Converter.convertHTML(document, options, "tiff-output.tiff");

When ImageFormat.Tiff is selected, use a .tif or .tiff extension for the output path so the file name matches the encoded image format.

Customize Image Output with ImageSaveOptions

The ImageSaveOptions class controls the format and rendering settings used during HTML-to-image conversion.

MethodUse it to
setFormat(value)Select JPG, PNG, BMP, GIF, or TIFF. The default format is PNG.
setBackgroundColor(value)Set the color that fills each output page. The default is transparent.
getPageSetup()Access the page setup used to configure output page size and margins.
setHorizontalResolution(value)Set horizontal resolution in pixels per inch. The default is 300 dpi.
setVerticalResolution(value)Set vertical resolution in pixels per inch. The default is 300 dpi.
setSmoothingMode(value)Configure image rendering quality.
setCompression(value)Select TIFF compression. The default TIFF compression is LZW.
getCss()Access CSS processing options.
getText()Access text rendering options.

The following example creates a JPG with 200 dpi horizontal and vertical resolution, an Antique White background, and a 500 × 250 pixel page with custom margins:

 1// Convert HTML to JPG in Java with with custom background, resolution, and page size settings
 2
 3// Prepare HTML code and save it to a file
 4String code = "<h1>  Image SaveOptions </h1> " +
 5        "<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of " +
 6        "conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>";
 7
 8FileHelper.writeAllText("save-options.html", code);
 9
10// Initialize an HTML Document from the HTML file
11HTMLDocument document = new HTMLDocument("save-options.html");
12
13// Initialize an ImageSaveOptions object and customize save options
14ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
15options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
16options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
17options.setBackgroundColor(Color.getAntiqueWhite());
18options.getPageSetup().setAnyPage(new Page(new Size(500, 250), new Margin(40, 40, 20, 20)));
19
20// Convert HTML to JPG
21Converter.convertHTML(document, options, "save-options-output.jpg");

For additional rendering controls, see Fine-Tuning Converters.

Related HTML Conversion Guides

Other Platforms

FAQ

Can I convert HTML to an image for free?

Yes. Use the free online HTML to Image Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Do HTML-to-image files preserve interactive HTML elements?

No. JPG, PNG, BMP, GIF, and TIFF output represents page content as raster image data, so links and form controls are not interactive in the resulting image. The examples on this page create static image output and do not demonstrate animation capture.

Try Online HTML to Image Conversion

Use the free online HTML to Image Converter for quick manual conversion without writing Java code.

Free Online HTML to JPG Converter