This simple how-to tutorial focuses on how to convert HTML to PDF in Python along with steps to configure the environment on your end. In order to generate PDF from HTML Python based API providing a simple API interface has been used. You may also customize the generated PDF by setting different options that are explained in the sections below.
Steps to Generate PDF from HTML in Python
- Setup the environment for using Aspose.Words for Python via .NET
- Include aspose.words namespace in your Python file
- Load the source HTML file into the Document class object
- Instantiate the PdfSaveOptions object and set the output PDF options
- Using the save method in Document class generate PDF from HTML
The aforementioned steps describe how can Python create PDF from HTML using simple API calls. It’s an easy process where after configuring the environment, you will load the source HTML file using the Document class object. The PdfSaveOptions will be used to customize the PDF and finally, it will be saved on the disk.
Code to Generate PDF from HTML in Python
In order to create PDF from HTML Python based converter application has been developed. You can also customize the output PDF with the help of PdfSaveOptions class. You can set the page number that you wish to export to PDF using page_set property. It also exhibits, how to set the Page mode and PDF compliance using page_mode and compliance properties respectively.
In this topic, we have learned about converting HTML to PDF in Python. If you are looking to create a Word document using Python, refer to the article on how to create Word document using Python.