Convert HTML Files, Strings, and Webpages to PDF
Aspose.HTML for Python via .NET
converts an HTML file, string, document, or URL to PDF with
Converter.convert_html()
and
PdfSaveOptions
. The conversion applies HTML and CSS, then creates fixed-layout PDF pages for distribution, printing, or archiving.
PdfSaveOptions controls page setup, CSS processing, background color, PDF metadata, JPEG compression quality for embedded raster content, tagged output, and encryption. Configure only the settings required by the destination workflow.
Test HTML to PDF Conversion and View the Python Code
Choose an HTML file or webpage URL, then keep PDF selected to create a fixed-layout document. The widget provides matching Python code for the selected source and output format.
{{#if_output 'PDF' 'DOCX' 'XPS' 'MHTML' 'MD' }}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'XHTML'}}
import aspose.html as ah
import aspose.html.saving as sav
{{/if_output}}
with ah.HTMLDocument("{{input lower}}") as document:
{{#if_output 'PDF'}}
options = sav.PdfSaveOptions()
{{/if_output}}
{{#if_output 'DOCX'}}
options = sav.DocSaveOptions()
{{/if_output}}
{{#if_output 'XPS'}}
options = sav.XpsSaveOptions()
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
{{#if_output 'MHTML'}}
options = sav.MHTMLSaveOptions()
{{/if_output}}
{{#if_output 'MD'}}
options = sav.MarkdownSaveOptions.default
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
conv.Converter.convert_html(document, options, "output.{{output lower}}")
{{/if_output}}
{{#if_output 'XHTML'}}
options = sav.HTMLSaveOptions()
options.document_type = sav.HTMLSaveOptions.XHTML
document.save("output.xhtml", options)
{{/if_output}}
The default example converts document.html to output.pdf with PdfSaveOptions. Add page, CSS, metadata, form, or security settings to the options object before calling convert_html() when the default PDF does not meet the destination requirements.
Python Code to Convert HTML to PDF
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
with ah.HTMLDocument("document.html") as document:
options = sav.PdfSaveOptions()
conv.Converter.convert_html(document, options, "output.pdf")
How to Convert HTML to PDF in Python
- Install the
aspose-html-netpackage. - Load an HTML file, string, document, or webpage URL. Provide a base URI when HTML text contains relative resource references.
- Create a
PdfSaveOptionsinstance. - Configure page layout, CSS media, metadata, or security when required.
- Call
Converter.convert_html()with the source, options, and PDF output path.
Configure PDF Pages, Metadata, and Security
Use PdfSaveOptions to adapt the generated PDF to its distribution or document-management requirements:
- Page layout and CSS: Set dimensions, margins, page rules, background, and CSS media type. Print CSS can apply print margins, page breaks, or hidden navigation; screen CSS keeps the result closer to the normal webpage layout.
- Embedded raster images: Use
jpeg_qualityto control JPEG compression for raster content encoded as JPEG. It does not change vector text or shapes. - Metadata and tags: Set the title, author, subject, and keywords through
document_info. Enableis_tagged_pdfwhen the output needs a PDF tag structure. - Encryption: Configure passwords and permissions only when the distribution workflow requires protected output. Select an encryption algorithm appropriate for the security requirements.
- Source resources: Preserve the document location or provide a base URI for relative references. Keep remote assets and required fonts available until conversion is complete. Content added only after later interaction may not be present in the PDF.
See
Convert HTML to PDF in Python
and the
PdfSaveOptions API Reference
for available properties.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
HTML to PDF FAQ
1. Does HTML to PDF conversion require Chrome or another browser?
No. Aspose.HTML converts HTML to PDF through its own document-processing APIs. The Python application needs the package, its supported runtime, and access to the source resources, but it does not need Chrome or browser automation.
2. Can Python convert a webpage URL directly to PDF?
Yes. Pass an aspose.html.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 stylesheets, images, fonts, and other resources.
3. Can one HTML to PDF conversion combine several HTML files?
The standard convert_html() call processes one HTML source. Assemble the required content and resource references into one HTMLDocument before conversion, or convert independent sources to separate PDF files.
4. Can HTML form controls remain interactive in the converted PDF?
The PdfSaveOptions.form_field_behaviour controls whether generated PDF form fields remain interactive or are flattened into page content. Choose the behavior required by the destination workflow before conversion.
PDF and Web Document Conversion Guides
Continue with another PDF source or choose a different HTML output format.