MHTML to PDF Conversion with the Java API
Aspose.HTML for Java
converts MHTML archives to PDF with
Converter.convertMHTML()
. Open the .mht or .mhtml source as a stream, create PdfSaveOptions, and provide the destination path. The renderer processes the archived HTML and its stored resources to create fixed PDF pages.
MHTML to PDF Java Code Example
The example opens an MHTML archive, creates the PDF save options, and writes the result to output.pdf. A try-with-resources statement closes the input stream automatically.
Convert MHTML to PDF in Java
import com.aspose.html.converters.Converter;
import com.aspose.html.saving.PdfSaveOptions;
import java.io.FileInputStream;
// Open the MHTML archive
try (FileInputStream inputStream = new FileInputStream("input.mhtml")) {
// Create the PDF save options
PdfSaveOptions options = new PdfSaveOptions();
// Convert MHTML to PDF
Converter.convertMHTML(inputStream, options, "output.pdf");
}
Test MHTML to PDF Conversion Online
Upload an MHTML archive or provide its URL to review the PDF result before implementing the same conversion in a Java application.
MHTML to PDF API Steps
- Add Aspose.HTML for Java to the project with Maven or Gradle.
- Open the
.mhtor.mhtmlsource as aFileInputStream. - Create a
PdfSaveOptionsinstance. - Pass the stream, options, and output path to
Converter.convertMHTML(). - Close the stream after conversion. A try-with-resources statement handles this automatically.
Configure PDF Pages and Security
Use PdfSaveOptions to configure page size and margins, background color, internal image resolution, JPEG quality, CSS processing, and encryption. The PDF can reproduce resources stored in the MHTML archive. If the archive refers to an external resource that was not embedded, that resource must remain accessible during conversion.
For complete settings and examples, see
Convert MHTML to PDF in Java
and the
PdfSaveOptions 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 .
MHTML to PDF Conversion FAQ
1. Why can an image or stylesheet be missing from the converted PDF?
The resource may not be embedded in the MHTML archive. If the archive contains an external reference, the resource must still be accessible when Converter.convertMHTML() processes the file.
2. Can I control PDF page size and margins during MHTML conversion?
Yes. Configure the page setup through PdfSaveOptions before conversion. Page dimensions, margins, CSS, and available fonts affect the final pagination.
3. Can a PDF created from MHTML be password protected?
Yes. PdfSaveOptions supports PDF encryption settings. Configure the required passwords and permissions before calling Converter.convertMHTML().
4. Can I convert MHTML to PDF online for free?
Yes. Use the free online MHTML to PDF converter for a manual conversion. Use Aspose.HTML for Java for automated or server-side workflows.
Related Java Conversions
Continue with an MHTML image format or another PDF workflow.