Convert HTML to MHTML in Java

To convert HTML to MHTML in Java, load or provide the HTML source, create MHTMLSaveOptions, and call Converter.convertHTML(). Use resource handling options to control which linked pages and resources are included in the MHTML archive.

MHTML, also known as MIME HTML, stores HTML and handled external resources in a single web archive, commonly using the .mht or .mhtml extension. Aspose.HTML for Java provides convertHTML() overloads and MHTMLSaveOptions for creating and configuring MHTML output.

HTML and MHTML

FormatResource storageTypical use
HTMLUsually references stylesheets, images, and other resources by URL.Websites and documents whose resources are stored separately or loaded from the web.
MHTMLPackages the HTML and handled resources into one MIME-based archive.Saving, transferring, or archiving a web page as one file.

Convert HTML to MHTML in One Line of Java Code

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

1// Convert HTML to MHTML in one line using Java
2
3// Invoke the convertHTML() method to convert HTML code to MHTML file
4Converter.convertHTML("<h1>Hello, Word!</h1>", ".", new MHTMLSaveOptions(), "convert-with-single-line.mht");

The base URI ("." in the example) is used to resolve relative resources in the HTML string. The result is saved as convert-with-single-line.mht.

Convert an HTML File to MHTML

Follow these steps to convert an existing HTML file:

  1. Load the source HTML file with HTMLDocument.
  2. Create MHTMLSaveOptions.
  3. Call Converter.convertHTML(document, options, outputPath) to convert HTML to MHTML.

In this example, the input is drawing.html, and the result is saved as drawing-output.mht.

 1// Convert HTML to MHTML in Java
 2
 3// Initialize an HTML document from the file
 4HTMLDocument document = new HTMLDocument("drawing.html");
 5
 6// Initialize MHTMLSaveOptions
 7MHTMLSaveOptions options = new MHTMLSaveOptions();
 8
 9// Convert HTML to MHTML
10Converter.convertHTML(document, options, "drawing-output.mht");

Download complete examples and data files from GitHub.

Customize Resource Handling with MHTMLSaveOptions

The MHTMLSaveOptions class exposes resource handling settings for the archive. getResourceHandlingOptions().setMaxHandlingDepth(value) controls how deeply linked pages are processed. A depth of 1 handles pages directly referenced by the saved document, -1 handles all reachable pages, and the default value is 0.

The following example demonstrates the depth setting:

  1. Prepare a source HTML document that links to another HTML page.
  2. Create MHTMLSaveOptions and set the maximum handling depth to 1.
  3. Call Converter.convertHTML(sourcePath, options, outputPath) to convert the source and directly linked page to MHTML.

In this example, document.html links to document2.html, and the result is saved as output-options.mht.

 1// Convert HTML to MHTML with custom settings using Java
 2
 3// Prepare HTML code with a link to another file and save it to the file as "document.html"
 4String code = "<span>Hello, World!!</span> " +
 5        "<a href='document2.html'>click</a>";
 6FileHelper.writeAllText("document.html", code);
 7
 8// Prepare HTML code and save it to the file as "document2.html"
 9code = "<span>Hello, World!!</span>";
10FileHelper.writeAllText("document2.html", code);
11
12// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
13MHTMLSaveOptions options = new MHTMLSaveOptions();
14options.getResourceHandlingOptions().setMaxHandlingDepth(1);
15
16// Convert HTML to MHTML
17Converter.convertHTML("document.html", options, "output-options.mht");

For restrictions and other resource handling settings, see Fine-Tuning Converters.

Related HTML Conversion Guides

Other Platforms

FAQ

Can I convert HTML to MHTML for free?

Yes. Use the free online HTML to MHTML 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.

Does an MHTML file include every external resource?

Not necessarily. Included content depends on resource handling settings, URL restrictions, resource availability, and the maximum handling depth. Configure MHTMLSaveOptions for the archive scope required by your workflow.

Try Online HTML to MHTML Conversion

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

Free Online HTML to MHTML Converter