Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert MHTML to PDF in Java, open the MHTML source as a file stream, create PdfSaveOptions, and call
Converter.convertMHTML(). Use PdfSaveOptions to configure page size, background color, resolution, CSS processing, JPEG quality, and encryption.
MHTML stores HTML markup and handled web resources in one archive. Converting MHTML to PDF produces a fixed-layout document suitable for sharing, printing, and long-term distribution. Aspose.HTML for Java accepts MHTML input from a supported stream, file path, or URL through convertMHTML() overloads.
The shortest example opens the MHTML file and passes its stream, default PdfSaveOptions, and the PDF output path directly to Converter.convertMHTML().
1// Convert MHTML to PDF using Java
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Invoke the convertMHTML() method to convert MHTML to PDF
7Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");The sample converts sample.mht and saves the result as convert-by-two-lines.pdf.
Follow these steps for a basic MHTML-to-PDF conversion:
FileInputStream.Converter.convertMHTML(stream, options, outputPath) to convert MHTML to PDF.The following example converts sample.mht to sample-output.pdf.
1// Convert MHTML to PDF using Java
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Create an instance of the PdfSaveOptions class
7PdfSaveOptions options = new PdfSaveOptions();
8
9// Call the convertMHTML() method to convert MHTML to PDF
10Converter.convertMHTML(fileInputStream, options, "sample-output.pdf");Download complete Java examples and data files from GitHub.
The PdfSaveOptions class controls PDF-specific conversion settings.
| Method | Use it to |
|---|---|
setJpegQuality(value) | Set JPEG compression quality when JPEG compression is used. The default is 95. |
setBackgroundColor(value) | Set the color that fills each output page. The default is transparent. |
getPageSetup() | Access the page setup used to configure output page size and margins. |
setHorizontalResolution(value) | Set horizontal resolution for internal images. The default is 300 dpi. |
setVerticalResolution(value) | Set vertical resolution for internal images. The default is 300 dpi. |
getCss() | Access CSS processing options. |
setEncryption(value) | Set PDF encryption details. No encryption is applied when this option is not set. |
The following workflow converts MHTML to PDF with custom page settings:
PdfSaveOptions and set the required background color.PageSetup and configure the output page dimensions.Converter.convertMHTML().The example converts sample.mht to sample-options.pdf, sets an Alice Blue background, and uses a 3000 × 1000 pixel page.
1// Convert MHTML to PDF in Java with custom settings
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
7PdfSaveOptions options = new PdfSaveOptions();
8options.setBackgroundColor(Color.getAliceBlue());
9options.getPageSetup().setAnyPage(new Page());
10options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(3000), Length.fromPixels(1000)));
11
12// Call the convertMHTML() method to convert MHTML to PDF
13Converter.convertMHTML(fileInputStream, options, "sample-options.pdf");For additional conversion controls, see Fine-Tuning Converters.
Yes. Use the free online MHTML to PDF Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.
The PDF can reproduce resources contained in the MHTML archive. If the archive references a resource that was not embedded, the resource must still be accessible during conversion. Check the source archive and any remaining external references when images or styles are missing.
Use the free online MHTML to PDF Converter for quick manual conversion without writing Java code.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.