Convert HTML to MHTML in Java

HTML files often link to external resources such as images, videos, audio, and other documents by specifying their URLs. If you want to archive or save a web page containing such external resources, you need to save them all separately along with the HTML file, which makes it a bit complicated and cluttered. MHTML is a file format that combines HTML with external resources into a single .mht file. MHTML uses the MIME email protocol to combine web page elements into a single archive file, making it easier to archive web pages.

This article explains how to convert HTML to MHTML using Aspose.HTML for Java library and apply MHTMLSaveOptions.

HTML to MHTML by a single line of Java code

The static methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to MHTML in your Java application literally with a single line of code!

1// Invoke the convertHTML() method to convert the HTML code to MHTML file
2Converter.convertHTML("<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), $o("convert-with-single-line.mht"));

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 MHTMLSaveOptions class, and the output file path where the converted file will be saved.

Convert HTML to MHTML in Java

Converting a file to another format using the convertHTML() method is a sequence of operations among which document loading and saving. The next example explains how to convert HTML to MHTML by line by line:

  1. Load an HTML file using HTMLDocument class.
  2. Create an instance of the MHTMLSaveOptions class.
  3. Use the convertHTML(document, options, savePath) method of the Converter class and pass the required parameters to it.
1// Initialize an HTML document from the file
2HTMLDocument document = new HTMLDocument($i("drawing.html"));
3
4// Initialize MHTMLSaveOptions
5MHTMLSaveOptions options = new MHTMLSaveOptions();
6
7// Convert HTML to MHTML
8Converter.convertHTML(document, options, $o("drawing-output.mht"));

You can download the complete examples and data files from GitHub.

Convert HTML to MHTML using MHTMLSaveOptions

Aspose.HTML allows converting HTML to MHTML using default or custom save options. MHTMLSaveOptions usage enables you to customize the rendering process. You can specify the UrlRestriction, MaxHandlingDepth, etc. To convert HTML to MHTML with MHTMLSaveOptions specifying, you should follow a few steps:

  1. Load or prepare an HTML file. In this example we create a source HTML file from scratch.
  2. Create a new MHTMLSaveOptions object.
  3. Use the convertHTML() method of the Converter class to save HTML document as an MHTML file.
 1// Prepare an HTML code with a link to another file and save it to the file as 'document.html'
 2String code = "<span>Hello, World!!</span> " +
 3        "<a href='document2.html'>click</a>";
 4FileHelper.writeAllText($o("document.html"), code);
 5
 6// Prepare HTML code and save it to the file as 'document2.html'
 7code = "<span>Hello, World!!</span>";
 8FileHelper.writeAllText($o("document2.html"), code);
 9
10// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
11MHTMLSaveOptions options = new MHTMLSaveOptions();
12options.getResourceHandlingOptions().setMaxHandlingDepth(1);
13
14// Convert HTML to MHTML
15Converter.convertHTML($o("document.html"), options, $o("output-options.mht"));

The MHTMLSaveOptions() constructor initializes an instance of the MHTMLSaveOptions class that is passed to convertHTML() method. The method takes the document, options, output file path savePath and performs the conversion. In the above example, we use the setMaxHandlingDepth(value) method that sets the maximum depth of resource which will be handled. The value = 1 means that only pages directly referenced from the saved document will be handled. Default value is 3.

Conclusion

MHTML simplifies the process of archiving and sharing web pages by combining HTML and all external resources into a single file, eliminating the complexity of managing individual files. Aspose.HTML for Java offers a simple solution for converting HTML to MHTML using the MHTMLSaveOptions class.

With its customizable features, you can:

The Java examples provided demonstrate both basic and advanced scenarios, allowing developers to effectively implement HTML to MHTML conversion in their Java applications.

To learn more about MHTMLSaveOptions please read Fine-Tuning Converters article.

Aspose.HTML offers a free online HTML to MHTML Converter that converts HTML to MHTML with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “Banner HTML to MHTML Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.