How to Convert HTML to PDF in Python

This quick tutorial guides on how to convert HTML to PDF in Python. You will get the basic information to set the development environment, a list of steps to accomplish the task, and a runnable sample code to convert HTML file to PDF using Python. Different options to customize the loading of the HTML file will also be discussed.

Steps to Transform HTML to PDF using Python

  1. Set the environment to add Aspose.PDF for Python via .NET to render HTML
  2. Create an object of aspose.pdf.HtmlLoadOptions class to control the loading of the HTML
  3. Load the source HTML file into the Document object using the load options mentioned above
  4. Save the resultant PDF file having source HTML contents in it

These steps describe the process to convert HTML to PDF in Python. First, an object of aspose.pdf.HtmlLoadOptions class object is created that is required by the Document class constructor. Once the source HTML file is loaded, you have complete control over the loaded file and then save it on the disk as a PDF file.

Code to Convert HTML to PDF using Python

This code demonstrates the process to change HTML to PDF with Python. The HtmlLoadOptions object can be used to set the warning handlers for any errors raised during the conversion, a flag to render the entire HTML file to a single PDF page, a flag to embed the fonts into the output PDF, and set the page layout to list a few. Similarly, when the PDF document object is instantiated, all the properties and methods to customize the output PDF file are available like setting the flag to hide the menubar when the output PDF file is opened, hiding the toolbar, and hiding the user interface elements to name a few.

This article has clarified the process to save HTML to PDF in Python. If you are inclined to learn the conversion of PDF to text, refer to the article on how to convert PDF to text in Python.

 English