Convert MHTML to PDF in Java

The ability to easily and reliably convert MHTML documents to other formats is one of the main features of Aspose.HTML for Java. PDF comes with many benefits, and MHTML to PDF conversion can be used for sharing, archiving, or printing web pages. In this article, you will find information about MHTML to PDF conversion scenarios and learn how to use PdfSaveOptions.

MHTML to PDF in a few lines of code

The static methods of the Converter class are primarily used as the easiest way to convert an MHTML file into various formats. You can convert MHTML to PDF with just a few lines of code!

1// Open an existing MHTML file for reading.
2java.io.FileInputStream fileInputStream = new java.io.FileInputStream($i("sample.mht"));
3
4// Invoke the ConvertMHTML() method to convert MHTML to PDF
5Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), $o("convert-by-two-lines.pdf"));

Convert MHTML to PDF in Java

Let’s walk through the step-by-step instructions for a simple MHTML to PDF conversion scenario:

  1. Load an MHTML file. You can load MHTML from a file, stream, or URL. In the example, we use the FileInputStream class to read an MHTML file as a stream of bytes.
  2. Create a new PdfSaveOptions object.
  3. Use the сonvertMHTML(stream, options, savePath) method of the Converter class to save an MHTML document as a PDF file. The method takes as parameters stream, options, and savePath and performs the conversion.

Please review the following Java code snippet, which shows the MHTML to PDF conversion process with step-by-step instructions:

1// Open an existing MHTML file for reading.
2java.io.FileInputStream fileInputStream = new java.io.FileInputStream($i("sample.mht"));
3
4// Create an instance of PdfSaveOptions.
5PdfSaveOptions options = new PdfSaveOptions();
6
7// Call the ConvertMHTML() method to convert MHTML to PDF
8Converter.convertMHTML(fileInputStream, options, $o("sample-output.pdf"));

You can download the complete examples and data files from GitHub.

Save Options – PdfSaveOptions Class

Aspose.HTML for Java allows converting MHTML to PDF using default or custom save options. PdfSaveOptions allows you to customize the rendering process. You can specify the page size, margins, file permissions, media type, etc.

MetodDescription
setJpegQuality(value)Specifies the quality of JPEG compression for images. The default value is 95.
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setEncryptionThis method gets or sets encryption details. If it is not set, then no encryption will be performed.

For further information on how to customize the conversion process with PdfSaveOptions, you can refer to the Fine-Tuning Converters article.

Convert MHTML to PDF using PdfSaveOptions

With Aspose.HTML for Java, you can convert files programmatically with full control over a wide range of conversion parameters. To convert MHTML to PDF with PdfSaveOptions specifying, you should follow a few steps:

  1. Load an MHTML file.
  2. Create a new PdfSaveOptions object and specify the required properties. In the following example, we apply a custom page size and background color for the resulting PDF document:
    • Use the setPageSetup() method to specify the page size for the output PDF document.
    • Use the setBackgroundColor() method to set the color that fills the background.
  3. Call the convertMHTML(stream, options, savePath) method of the Converter class.
 1// Open an existing MHTML file for reading.
 2java.io.FileInputStream fileInputStream = new java.io.FileInputStream($i("sample.mht"));
 3
 4// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
 5PdfSaveOptions options = new PdfSaveOptions();
 6options.setBackgroundColor(Color.getAliceBlue());
 7options.getPageSetup().setAnyPage(new Page());
 8options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(3000), Length.fromPixels(1000)));
 9
10// Call the ConvertMHTML() method to convert MHTML to PDF
11Converter.convertMHTML(fileInputStream, options, $o("sample-options.pdf"));

Conclusion

Aspose.HTML for Java offers users a versatile and robust API for converting MHTML to PDF. The conversion process involves loading an MHTML file, creating a PdfSaveOptions object with default or custom options, and using the convertMHTML() methods of the Converter class to perform the conversion. These simple steps allow you to create accurate, high-quality PDF documents that suit your needs.

Aspose.HTML offers a free online MHTML to PDF Converter that converts MHTML to PDF with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Text “MHTML to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.