Convert MHTML to PDF in Python – Aspose.HTML for Python via .NET
In this guide, you will find information on how to convert an MHTML document into a Portable Document Format (PDF) file format using Aspose.HTML for Python via .NET library. We are going to cover in detail how to convert MHTML to PDF using the convert_mhtml() methods of the Converter class, and how to apply PdfSaveOptions.
To continue following this tutorial, install and configure the Aspose.HTML for Python via .NET in your Python project. Our code examples help you to convert HTML to PDF and generate PDF files using the Python library.
Online MHTML Converter
You can test the Python API functionality and convert MHTML in real-time. Load an MHTML file from your local file system, select the output format and run the example. The conversion will be performed with default save options. You will immediately receive the result as a separate file.
Convert MHTML to PDF using PdfSaveOptions
Using Converter.convert_mhtml() methods is the most common way to convert MHTML code into various formats. With Aspose.HTML for Python via .NET, you can convert MHTML to PDF format programmatically with full control over a wide range of conversion parameters. You should follow a few steps:
- Open an existing MHTML file.
- Create an instance of the
PdfSaveOptions class. The
PdfSaveOptions()
constructor initializes anoptions
instance that is passed to convert_mhtml() method. - Use the
convert_mhtml() method to save MHTML as a PDF file. The convert_mhtml() method takes the
stream
,options
, output file pathsave_path
and performs the conversion operation.
The following Python code example shows how to use PdfSaveOptions and create a PDF file with custom save options:
1import os
2from aspose.html import *
3from aspose.html.converters import *
4from aspose.html.saving import *
5from aspose.html.drawing import *
6from aspose.pydrawing import *
7
8# Setup directories and define paths
9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12 os.makedirs(output_dir)
13document_path = os.path.join(input_dir, "document.mht")
14save_path = os.path.join(output_dir, "document.pdf")
15
16# Open an existing MHTML file for reading
17with open(document_path, "rb") as stream:
18
19 # Create an instance of PdfSaveOptions
20 options = PdfSaveOptions()
21 options.page_setup.any_page = Page(drawing.Size(800, 600), Margin(10, 10, 10, 10))
22 options.css.media_type.PRINT
23 options.jpeg_quality = 100
24
25 # Convert MHTML to PDF
26 Converter.convert_mhtml(stream, options, save_path)
In the above example, we use:
page_setup
property to specify the page size and margins (top, bottom, left, right);css.media_type
property that specify how CSS media queries are handled during the conversion process;jpeg_quality
property that determines the JPEG compression quality used for images embedded in a PDF document.
Save Options – PdfSaveOptions Class
The
PdfSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting MHTML to PDF format. Some properties of this class inherit properties of base classes, such as
PdfRenderingOptions or RenderingOptions. Here is a description of properties available in PdfSaveOptions
:
- page_setup – This property provides access to a PageSetup object used to configure the layout and settings of the output PDF pages to fit specific printing or display requirements.
- horizontal_resolution – This property controls the horizontal resolution for both internal images used during processing and any external images included in the HTML. By default, it is set to 300 dpi.
- vertical_resolution – Similar to horizontal_resolution, this property manages the vertical resolution for internal and external images during PDF generation. Like its horizontal counterpart, it defaults to 300 dpi.
- background_color – This property sets or retrieves the background color that fills each PDF document page. The default value is transparent, but this can be customized to suit branding or aesthetic preferences, ensuring consistency across all pages.
- css – This property uses a
CssOptions
object to configure the processing of CSS properties during HTML to PDF conversion. It allows precise control over how styles from the HTML are interpreted and applied in the resulting PDF. - jpeg_quality – This property determines the JPEG compression quality used for images embedded in a PDF document. The default quality is set to 95, providing a good balance between image fidelity and file size. Setting this property allows you to optimize file size or image quality based on your specific needs.
- encryption – This property provides detailed information about PDF document encryption, including password protection and permission settings. If it is not configured, no encryption is applied, but setting this property allows you to distribute and control access to sensitive PDF content securely.
- document_info – This property contains metadata and information about the output PDF document, such as title, author, subject, and keywords. This metadata helps document management, indexing, and searchability, making the PDF more informative and organized.
- form_field_behaviour – This property specifies the behavior of interactive form fields in the generated PDF.
- is_tagged_pdf – When set to true, a tagged layout is created within the PDF document, enhancing accessibility for users with disabilities. This ensures that content is properly structured and navigable using assistive technology and meets accessibility standards.
How to convert MHTML to XPS
Aspose.HTML for Python via .NET supports MHTML to XPS conversion. To do this, you should use
XpsSaveOptions to get a save options object that is passed to the convert_mhtml()
method:
options = XpsSaveOptions()
XpsSaveOptions usage enables you to customize the rendering process; you can specify the page_setup, background_color, css, horizontal_resolution, and vertical_resolution properties.
Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
Aspose.HTML offers a free online MHTML to PDF Converter that converts MHTML to PDF file with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!