Convert HTML to PDF in Python
A PDF file is a fixed-layout document that includes the text, graphics, hyperlinks, buttons, form fields, multimedia, and other information needed to display. PDFs are highly secure, allowing password protection, encryption, and digital signatures to safeguard sensitive information. They are also universally accessible and easily viewable on any device without specific software. Furthermore, PDFs are compact and can compress high-quality files into smaller sizes, making them ideal for sharing and storage.
In this guide, you will find information on how to convert an HTML document into a Portable Document Format (PDF) file format using Aspose.HTML for Python via .NET. We are going to cover in detail how to convert HTML to PDF using the convert_html() methods of the Converter class, and how to apply PdfSaveOptions. Also, you can try an Online HTML Converter to test the Aspose.HTML functionality and convert HTML on the fly.
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.
HTML to PDF with one line of code
The methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to PDF in your Python application literally with a single line of code!
1# Convert HTML to PDF using Python
2
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6# Convert HTML to PDF
7conv.Converter.convert_html("document.html", sav.PdfSaveOptions(), "output.pdf")Online HTML Converter
You can test the power of Aspose.HTML for Python via .NET and perform HTML conversion in real-time. Simply load an HTML file from your local file system or URL, select the desired output format, and run the provided code example. The example uses the default save options, allowing for a simple conversion process. Once completed, you will instantly receive the converted file in the format of your choice.
Convert HTML to PDF using PdfSaveOptions
With Aspose.HTML for Python via .NET, you can convert files programmatically with full control over a wide range of conversion parameters. To convert HTML to PDF with PdfSaveOptions specifying, you should follow a few steps:
- Load an HTML file using one of HTMLDocument() constructors of the HTMLDocument class. In the example above, we initialize an HTML document from a file.
- Create a new
PdfSaveOptions object and specify the required properties. The
PdfSaveOptionsclass provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to PDF. - Use the
convert_html() method of the Converter class. In the following example, you need to pass the
HTMLDocument,PdfSaveOptions, and output file path to theconvert_html()method.
The following Python code example shows how to use PdfSaveOptions and create a PDF file with custom save options:
1# Convert HTML to PDF in Python with custom settings
2
3import os
4import aspose.html as ah
5import aspose.html.converters as conv
6import aspose.html.saving as sav
7import aspose.html.drawing as dr
8import aspose.pydrawing as pd
9import aspose.html.rendering.pdf.encryption as rpe
10
11
12# Setup directories and define paths
13output_dir = "output/"
14input_dir = "data/"
15if not os.path.exists(output_dir):
16 os.makedirs(output_dir)
17document_path = os.path.join(input_dir, "document.html")
18save_path = os.path.join(output_dir, "document-options.pdf")
19
20# Load an HTML document from a file or URL
21doc = ah.HTMLDocument(document_path)
22
23# Initialize saving options
24options = sav.PdfSaveOptions()
25options.page_setup.any_page = dr.Page(dr.Size(800, 600), dr.Margin(10, 10, 10, 10))
26options.css.media_type.PRINT
27options.horizontal_resolution = dr.Resolution.from_dots_per_inch(100.0)
28options.vertical_resolution = dr.Resolution.from_dots_per_inch(100.0)
29options.background_color = pd.Color.bisque
30options.is_tagged_pdf = True
31options.jpeg_quality = 90
32
33doc_info = options.document_info
34doc_info.title = "Aspose HTML Example"
35doc_info.author = "Your Name"
36doc_info.subject = "PDF Conversion"
37doc_info.keywords = "Aspose, HTML, PDF"
38
39options.encryption = rpe.PdfEncryptionInfo(
40 user_password="user123",
41 owner_password="owner123",
42 permissions=rpe.PdfPermissions.PRINT_DOCUMENT | rpe.PdfPermissions.EXTRACT_CONTENT,
43 encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
44)
45
46# Convert HTML to PDF
47conv.Converter.convert_html(doc, options, save_path)We convert an HTML document to a PDF file using save options in this example. The process involves initializing the HTML document, setting custom save options such as page size and css media_type, and then performing the conversion. Finally, the converted PDF file is saved to a specified output directory.
You can evaluate the quality of conversion by trying our product. The following figure shows the result of converting an aspose.html file to PDF format:

PdfSaveOptions Class
Aspose.HTML for Python via .NET provides the
PdfSaveOptions class, which gives you more control over how documents are saved in PDF format. Some properties of this class inherit properties of base classes, such as
PdfRenderingOptions or RenderingOptions. PdfSaveOptions usage enables you to customize the rendering process; you can specify the page size, margins, file permissions, Css, etc. 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
CssOptionsobject 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. - 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.
- 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.
- 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.
HTML to PDF conversion and PDF flattening
Aspose.HTML for Python via .NET offers the
form_field_behaviour property of the PdfSaveOptions class to flatten PDF documents after their conversion from HTML or MHTML. This property is used to specify the behavior of form fields in a PDF document. If the value is set to FormFieldBehaviour.FLATTENED all form fields in the PDF document will be flattened.
1# Flatten PDF during HTML to PDF conversion using Python
2
3import os
4import aspose.html as ah
5import aspose.html.converters as conv
6import aspose.html.saving as sav
7import aspose.html.rendering.pdf as rp
8
9# Setup directories and define paths
10data_dir = "data/"
11output_dir = "output/"
12os.makedirs(output_dir, exist_ok=True)
13
14source_path = os.path.join(data_dir, "SampleHtmlForm.html")
15result_path = os.path.join(output_dir, "form-flattened.pdf")
16
17# Load an HTML document
18doc = ah.HTMLDocument(source_path)
19
20# Initialize PdfSaveOptions
21options = sav.PdfSaveOptions()
22options.form_field_behaviour = rp.FormFieldBehaviour.FLATTENED
23
24# Convert HTML to PDF
25conv.Converter.convert_html(doc, options, result_path)How to convert HTML to XPS
Aspose.HTML for Python via .NET supports HTML to XPS conversion. To do this, you should use
XpsSaveOptions to get a save options object that is passed to the convert_html() 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.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online HTML to PDF Converter that converts HTML to PDF with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!
