Convert HTML to MHTML Using 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           
2    com.aspose.html.converters.Converter.convertHTML("<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.combine(getOutputDir(), "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    // Prepare a path to a source HTML file
 2    String documentPath = Path.combine(getDataDir(), "drawing.html");
 4    // Prepare a path for converted file saving 
 5    String savePath = Path.combine(getOutputDir(), "drawing-output.mht");
 7    // Initialize an HTML document from the file
 8    HTMLDocument document = new HTMLDocument(documentPath);
 9    try {
10        // Initialize MHTMLSaveOptions
11        MHTMLSaveOptions options = new MHTMLSaveOptions();
13        // Convert HTML to MHTML
14        com.aspose.html.converters.Converter.convertHTML(document, options, savePath);
15    }
16    finally { if (document != null) ((IDisposable)document).dispose(); }

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 HTML code with a link to another file and save it to the file as 'document.html'
 2    String code = StringExtensions.concat("<span>Hello, World!!</span> ", 
 3                "<a href='document2.html'>click</a>");
 5    File.writeAllText("document.html", code);
 7    // Prepare HTML code and save it to the file as 'document2.html'
 8    code = "<span>Hello, World!!</span>";
 9    File.writeAllText(getOutputDir()+"/document2.html", code);
11    String savePath = Path.combine(getOutputDir(), "output-options.mht");
13    // Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
14    MHTMLSaveOptions options = new MHTMLSaveOptions();
15    options.getResourceHandlingOptions().setMaxHandlingDepth(1);
17    // Convert HTML to MHTML
18    com.aspose.html.converters.Converter.convertHTML("document.html", options, savePath);

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.

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.