Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to PDF in Python, call Converter.convert_html() with the HTML source, PdfSaveOptions, and an output path. The source can be an HTML file, an HTMLDocument, inline HTML with a base URI, or a URL supported by the selected overload.
Aspose.HTML for Python via .NET renders HTML and CSS to fixed-layout PDF output. The following examples demonstrate conversion from a file and a webpage URL, custom PDF settings, and form-field flattening.
The shortest file-based workflow uses a source path and the default PDF save settings:
PdfSaveOptions object.Converter.convert_html() with the source, options, and output PDF path.1# Convert HTML to PDF in 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")This overload reads the HTML file, applies the default PDF settings, and writes output.pdf.
Use a Url object when the HTML source is an online webpage:
Url object for the webpage.PdfSaveOptions and specify an output path.Converter.convert_html(). 1import os
2import aspose.html as ah
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6output_dir = "output"
7os.makedirs(output_dir, exist_ok=True)
8
9url = ah.Url("https://docs.aspose.com/html/files/document.html")
10output_path = os.path.join(output_dir, "document-from-url.pdf")
11
12options = sav.PdfSaveOptions()
13conv.Converter.convert_html(url, options, output_path)The application must have network access to the webpage and its linked resources. This example uses a small static HTML file so that the result is predictable. A complex website can take considerably longer to process while its stylesheets, images, fonts, scripts, and other resources are loaded.
Use PdfSaveOptions when the PDF requires custom page settings, background color, metadata, form-field behavior, or password and permission settings.
HTMLDocument.PdfSaveOptions and configure only the settings required by the output.Converter.convert_html().The following example configures page size and margins, background color, PDF metadata, and password-based permissions.
1import os
2import aspose.html as ah
3import aspose.html.converters as conv
4import aspose.html.drawing as dr
5import aspose.html.saving as sav
6import aspose.pydrawing as pd
7import aspose.html.rendering.pdf.encryption as rpe
8
9input_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13input_path = os.path.join(input_dir, "document.html")
14output_path = os.path.join(output_dir, "document-options.pdf")
15
16with ah.HTMLDocument(input_path) as document:
17 options = sav.PdfSaveOptions()
18 options.page_setup.any_page = dr.Page(
19 dr.Size(800, 600),
20 dr.Margin(10, 10, 10, 10)
21 )
22 options.background_color = pd.Color.bisque
23
24 document_info = options.document_info
25 document_info.title = "Aspose HTML Example"
26 document_info.author = "Your Name"
27 document_info.subject = "PDF Conversion"
28 document_info.keywords = "Aspose, HTML, PDF"
29
30 options.encryption = rpe.PdfEncryptionInfo(
31 user_password="user123",
32 owner_password="owner123",
33 permissions=(
34 rpe.PdfPermissions.PRINT_DOCUMENT
35 | rpe.PdfPermissions.EXTRACT_CONTENT
36 ),
37 encryption_algorithm=rpe.PdfEncryptionAlgorithm.RC4_128
38 )
39
40 conv.Converter.convert_html(document, options, output_path)The numeric page size and margin values in this example are measured in pixels. The generated PDF uses the specified user and owner passwords and permits printing and content extraction.
PdfSaveOptions inherits common rendering settings and adds PDF-specific controls. The most relevant properties are:
| Property | Use it to control |
|---|---|
| page_setup | PDF page size, margins, and page layout. |
| css | CSS processing settings, including the media type used during rendering. |
| background_color | The color that fills each PDF page. The default is transparent. |
| horizontal_resolution and vertical_resolution | Resolution of raster images generated for graphical filter effects. The default is 300 dpi. These properties do not define an overall DPI for the PDF. |
| jpeg_quality | JPEG compression quality when JPEG compression is used. The default is 95; increasing it does not improve the source image resolution. |
| document_info | PDF metadata such as the title, author, subject, and keywords. |
| form_field_behaviour | Whether generated PDF form fields remain interactive or are flattened. |
| encryption | Passwords, permissions, and encryption details for the output PDF. |
| is_tagged_pdf | Creation of a tag structure in the PDF. Tagging alone does not guarantee compliance with an accessibility standard. |
Set form_field_behaviour to FormFieldBehaviour.FLATTENED when form fields in the generated PDF should no longer be interactive:
PdfSaveOptions and select flattened form-field behavior.Converter.convert_html(). 1# Convert HTML to PDF and flatten form fields in 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# Prepare input and output 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 the HTML form
18with ah.HTMLDocument(source_path) as doc:
19
20 # Flatten form fields in the output PDF
21 options = sav.PdfSaveOptions()
22 options.form_field_behaviour = rp.FormFieldBehaviour.FLATTENED
23
24 # Convert the HTML form to PDF
25 conv.Converter.convert_html(doc, options, result_path)The same Converter API can produce XPS instead of PDF. Use XpsSaveOptions to configure XPS output:
XpsSaveOptions object.Converter.convert_html() with the source, options, and output XPS path.1import aspose.html.converters as conv
2import aspose.html.saving as sav
3
4options = sav.XpsSaveOptions()
5conv.Converter.convert_html("document.html", options, "document.xps")Use this direct conversion workflow when you only need to convert HTML to XPS. Use XpsDevice when the application requires explicit control over the rendering device.
XpsSaveOptions.Yes. Pass the HTML string, a base URI, PdfSaveOptions, and the output path to Converter.convert_html(). The base URI lets the converter resolve relative URLs for stylesheets, images, fonts, and other resources.
Yes. Pass an
Url object to a supported Converter.convert_html() overload, or load the webpage into an HTMLDocument before conversion. The application must be able to access the page and its linked resources.
Relative resource URLs must resolve against the document location or a valid base URI. Remote resources must be accessible, and fonts used by the page must be available to the rendering environment. Also check whether the page defines different styles for screen and print media.
No. Setting is_tagged_pdf to True creates a PDF tag structure, but accessibility also depends on the source document structure, alternative text, reading order, form labels, and other content requirements.
Use the free online HTML to PDF Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when HTML-to-PDF conversion must run in a Python application, service, or document-processing workflow.
Download complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.