MHTML to PDF Conversion

In this article, you will find information about how to convert an MHTML to PDF and how to use PdfSaveOptions and MemoryStreamProvider parameters.

The static methods of the Converter class are primarily used as the easiest way to convert an MHTML code into various formats. You can convert MHTML to PDF in your Java application literally with a single line of code!

The following Java code snippet shows how to convert MHTML to PDF using Aspose.HTML for Java.

  1. Open an existing MHTML file;
  2. Create an instance of PdfSaveOptions;
  3. Use the Converter.convertMHTML method of Converter class to save MHTML as a PDF file. You need to pass the MHTML file stream, PdfSaveOptions, and output file path to the Converter.convertMHTML method to convert MHTML to PDF.

Save Options

PdfSaveOptions allows you to customize the rendering process. You can specify the page size, margins, file permissions, CSS media-type, etc. The following example shows how to use PdfSaveOptions and create a PDF file with custom page-size and background color:

Please note that you cannot set values against the Application and Producer fields, because Aspose Ltd. and Aspose.HTML for Java x.x.x will be displayed against these fields.

To learn more about PdfSaveOptions please read Fine-Tuning Converters article.

Output Stream Providers

If it is required to save files in the remote storage (e.g., cloud, database, etc.) you can implement MemoryStreamProvider interface to have manual control over the file creating process. This interface designed as a callback object to create a stream at the beginning of the document/page (depending on the output format) and release the early created stream after rendering document/page.

The example below shows how to implement and use the your own MemoryStreamProvider in the application: