Create an MHTML Archive from HTML

Aspose.HTML for Java converts HTML to MHTML with Converter.convertHTML() . Load the page with HTMLDocument, create MHTMLSaveOptions, and specify the archive path. MHTML uses MIME parts to store the main HTML and handled resources in one .mht or .mhtml file.


Java Code for Single-File Web Archiving

The example loads document.html, creates the MHTML save options, and writes the page and handled resources to output.mhtml.

Convert HTML to MHTML in Java

import com.aspose.html.HTMLDocument;
import com.aspose.html.converters.Converter;
import com.aspose.html.saving.MHTMLSaveOptions;

// Load the HTML document
try (HTMLDocument document = new HTMLDocument("document.html")) {
    // Create the MHTML save options
    MHTMLSaveOptions options = new MHTMLSaveOptions();

    // Convert HTML to MHTML
    Converter.convertHTML(document, options, "output.mhtml");
}

Test HTML to MHTML Archiving Online

Upload an HTML file or provide a webpage URL to inspect the MHTML archive before implementing the same operation in Java.

HTML to MHTML API Workflow

  1. Add Aspose.HTML for Java to the project.
  2. Load the HTML file or URL with HTMLDocument .
  3. Create an MHTMLSaveOptions instance.
  4. Pass the document, options, and output path to Converter.convertHTML() .

Control Which Resources Enter the Archive

MHTMLSaveOptions exposes resource-handling settings that control URL restrictions and the maximum depth of linked pages to process. Resource availability and access rules determine which images, stylesheets, scripts, fonts, and linked content can be included. Use these settings to keep the archive within the intended scope.

See Convert HTML to MHTML in Java and the MHTMLSaveOptions API Reference .


Install Aspose.HTML for Java

Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the Aspose.HTML for Java releases page, then reload or rebuild the project so the dependency is resolved.

Add the repository and dependency to the Maven project:

Maven – pom.xml

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <url>https://releases.aspose.com/java/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>ASPOSE_HTML_VERSION</version>
    </dependency>
</dependencies>

Add the repository and dependency to the Gradle project:

Gradle – build.gradle

repositories {
    maven {
        url = uri('https://releases.aspose.com/java/repo/')
    }
}
dependencies {
    implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}

Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .


HTML to MHTML Conversion FAQ

1. Does MHTML include images and CSS used by the HTML page?

It can package handled resources such as images and stylesheets into MIME parts. A resource must be reachable and permitted by the configured resource-handling rules to enter the archive.

2. Why can an MHTML archive still miss a webpage resource?

The resource may be inaccessible, blocked, dynamically loaded, outside the allowed URL scope, or beyond the configured handling depth. Review the source URLs and MHTMLSaveOptions settings.

3. What is the difference between .mht and .mhtml?

Both extensions are used for MIME HTML web archives. The archive structure is the same type of format; the preferred extension usually depends on the consuming application or workflow.

4. Can I convert HTML to MHTML online for free?

Yes. Use the free online HTML to MHTML converter for a manual archive. Use Aspose.HTML for Java for automated web archiving.

Related Java Conversions

Choose another output format for the same HTML source.