Convert MHTML to PDF in 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 and MemoryStreamProvider.
MHTML to PDF by a single line 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 single line of code!
1 // Invoke the сonvertMHTML() method to convert MHTML to PDF
2 com.aspose.html.converters.Converter.convertMHTML(File.openRead(StringExtensions.concat(getDataDir(), "sample.mht")), new PdfSaveOptions(), Path.combine(getOutputDir(), "output.pdf"));
Convert MHTML to PDF in Java
Let’s walk through the step-by-step instructions for a simple MHTML to PDF conversion scenario:
- Load an MHTML file. You can load MHTML from a file, stream, or URL. In the example we use
openRead(path)
method to open and read an MHTML document from the file system. - Create a new PdfSaveOptions object.
- Use the
сonvertMHTML(stream, options, savePath)
method of the Converter class to save an MHTML document as a PDF file. The method takes as parametersstream
,options
, andsavePath
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
2 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "sample.mht"));
3 try
4 {;
5 }
6 finally { if (stream != null) ((IDisposable)stream).dispose(); }
7
8 // Prepare a path to save the converted file
9 String savePath = Path.combine(getOutputDir(), "sample-output.pdf");
10
11 // Create an instance of PdfSaveOptions
12 PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
13
14 // Convert MHTML to PDF
15 com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);
You can download the complete examples and data files from GitHub.
Save Options
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, CSS media-type, etc.
Metod | Description |
---|---|
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. |
setEncryption | This 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.
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.
Convert MHTML to PDF in Java 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:
- Load an MHTML file.
- 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.
- Use the
- Call the
сonvertMHTML(stream, options, savePath)
method of the Converter class.
The following Java example shows how to use PdfSaveOptions and create a PDF file with custom page size and background color:
1 // Open an existing MHTML file for reading
2 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "sample.mht"));
3 try
4 {;
5 }
6 finally { if (stream != null) ((IDisposable)stream).dispose(); }
7
8 // Prepare a path to save the converted file
9 String savePath = Path.combine(getOutputDir(), "sample-options.pdf");
10
11 // Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
12 PdfSaveOptions options = new PdfSaveOptions();
13 PageSetup pageSetup = new PageSetup();
14 Page anyPage = new Page();
15 anyPage.setSize(new com.aspose.html.drawing.Size(Length.fromPixels(1000),Length.fromPixels(1000)));
16 pageSetup.setAnyPage(anyPage);
17 options.setPageSetup(pageSetup);
18 options.setBackgroundColor(Color.getAliceBlue());
19
20 // Convert MHTML to PDF
21 com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);
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!