HTML to MHT Java

MHT files are used to keep images, text, CSS, and other related resources in a single webpage. In some use cases, you might want to convert an HTML file to MHT format. Accordingly, this article covers how to convert HTML to MHT format programmatically in Java.

HTML to MHT File Converter – Java API Installation

Aspose.HTML for Java API can be used to work with HTML, MHTML, XPS, and other supported file formats. You can download the JAR files from the Downloads page or using the following configurations in the pom.xml file of your application:

Repository:

 <repositories>
     <repository>
         <id>snapshots</id>
         <name>repo</name>
         <url>http://repository.aspose.com/repo/</url>
     </repository>
</repositories>

Dependency:

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>21.12</version>
        <classifier>jdk17</classifier>
    </dependency>
</dependencies>

Convert HTML to MHT or MHTML in Java

You need to follow the steps below for converting an HTML page to an MHT or MHTML file:

  1. Create an object of the MHTMLSaveOptions class.
  2. Save the output MHT or MHTML file with the ConvertHTML method.

The code sample below explains how to convert an HTML file to MHT or MHTML file programmatically in Java:

// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("input.html");
// Initialize MHTMLSaveOptions object
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Convert HTML to MHTML
Converter.convertHTML(document, options, "output.mht");

Convert HTML to MHT with Advanced Options in Java

You can enhance the process of converting the HTML file to MHT format by using the properties exposed by the MHTMLSaveOptions class. Please follow the steps below for converting HTML to an MHT file with advanced options:

  1. Initialize an instance of the MHTMLSaveOptions class.
  2. Specify the properties for the output MHT file.
  3. Save the HTML file as MHT with the ConvertHTML method.

The code sample below demonstrates how to convert an HTML file to MHT format programmatically in Java:

// Prepare an HTML code with a link to another file and save it to the file as 'document.html'
String code = "<span>Hello World!!</span>\n" +
"<a href='document2.html'>click</a>\n";
try (FileWriter fileWriter = new FileWriter("document.html")) {
fileWriter.write(code);
}
// Prepare an HTML code and save it to the file as 'document2.html'
code = "<span>Hello World!!</span>";
try (FileWriter fileWriter = new FileWriter("document2.html")) {
fileWriter.write(code);
}
// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources.
MHTMLSaveOptions options = new MHTMLSaveOptions();
options.getResourceHandlingOptions().setMaxHandlingDepth(1);
// Convert HTML to MHT
Converter.convertHTML(
"document.html",
options,
"output.mht"
);
}
}

Explore Aspose.HTML for Java API

You might visit the API documentation to understand many other features supported by the API.

Get Free License

You can evaluate the API in its full capacity by requesting a free temporary license.

Conclusion

In this article, you have learned how to convert an HTML file to MHT or MHTML file programmatically in Java. Moreover, it also covers the advanced features to customize the conversion process. Please feel free to reach out to us at the forum in case of any inquiries.

See Also

Convert EPUB to XPS in Java