IronPDF How-Tos Create PDF How to Create PDF Files in Python Chaknith Bin Updated:July 28, 2025 Automating the creation of PDF documents using Python enables developers to generate PDFs as part of their applications. This capability proves beneficial in various scenarios, including the generation of invoices, reports, or other types of PDFs as needed. This How-To Guide focuses on utilizing IronPDF to programmatically create PDF files within Python Scripts. How to Create PDF File in Python Install Python library to create PDF files Utilize the RenderHtmlAsPdf method to transform an HTML string into a PDF document Use the RenderHtmlFileAsPdf method to generate a PDF file directly from an HTML file Leverage the RenderUrlAsPdf method to create PDF files from URL Export password-protected PDF Files to desired directory Python PDF Library: IronPDF IronPDF is a powerful Python library specifically designed for creating PDF documents from HTML. Its user-friendly APIs make it easy to generate and customize PDFs with various features, including: Adding text, images, and other types of content Choosing fonts, colors, and controlling document layout and formatting. IronPDF can be seamlessly integrated into .NET, Java, and Python applications, enabling versatile PDF generation across multiple platforms. In addition to its powerful PDF generation capabilities, IronPDF offers a wide range of features. These encompass file format conversion, efficient text and data extraction from PDFs, and the ability to secure PDFs through password encryption. Steps to Create PDF Document in a Python Script Prerequisites To use IronPDF for Python, please ensure that the computer has the following prerequisite software installed: .NET 6.0 SDK: To use IronPDF for Python, you need the .NET 6.0 SDK installed on your machine as it relies on the IronPDF .NET library. Download the .NET 6.0 SDK from the official Microsoft website. Python: Download and install the latest version of Python 3.x from the official Python website: https://www.python.org/downloads/. During the installation process, make sure to select the option to add Python to the system PATH, which will make it accessible from the command line. Pip: Pip is usually bundled with Python installation starting from Python 3.4 and later. However, depending on your Python installation, you may need to check if pip is already installed or install it separately. IronPDF Library: The IronPDF library can be installed using pip. Use the command below to install IronPDF using pip: pip install ironpdf Please noteOn some systems, Python 2.x may still be the default version. In such cases, you may need to explicitly use the pip3 command instead of pip to ensure that you're using Pip for Python 3. Important steps before writing code First, add the statement below to the top of the Python script. # Import statement for IronPDF for Python from ironpdf import * # Import statement for IronPDF for Python from ironpdf import * PYTHON Next, configure IronPDF with a valid license key by assigning the license key to the LicenseKey attribute of License (before any other lines of code). # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" PYTHON Please noteTo create PDFs without any watermarks, you will need a valid license key. Purchase a license key or obtain a free trial license key. Otherwise, continue to the next step to generate new PDF documents for free with watermarks. Convert HTML String into PDF Document Use the RenderHtmlAsPdf method to generate a new PDF document from an HTML string. Simply provide the HTML markup as the parameter for the RenderHtmlAsPdf method. IronPDF will perform the conversion, resulting in a PdfDocument instance. # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an HTML string using Python pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an HTML string using Python pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>") PYTHON Once the HTML string has been successfully converted to a PDF document, use the SaveAs method to save the PDF to a path on the local system: # Export to a file or Stream pdf.SaveAs("htmlstring_to_pdf.pdf") # Export to a file or Stream pdf.SaveAs("htmlstring_to_pdf.pdf") PYTHON A PDF file named "htmlstring_to_pdf.pdf" will be created, preserving the contents of the original HTML string. Generate PDF from HTML file in Python To generate a PDF document from an HTML file stored locally in Python, follow the code provided below: # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an existing HTML file using Python pdf = renderer.RenderHtmlFileAsPdf("example.html") # Export to a file or Stream pdf.SaveAs("htmlfile_to_pdf.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an existing HTML file using Python pdf = renderer.RenderHtmlFileAsPdf("example.html") # Export to a file or Stream pdf.SaveAs("htmlfile_to_pdf.pdf") PYTHON In the code snippet above, the RenderHtmlFileAsPdf method is used to create a PDF document from an HTML file. You need to provide a string or path specifying the location of the HTML file on the filesystem. IronPDF renders the HTML elements, including any associated CSS and JavaScript, just like a web browser would. This ensures accurate representation of the content in the resulting PDF. Finally, use the SaveAs method to save the generated PDF to a specific location on your system, similar to the previous example. Create PDF from URL in Python To create a PDF document from a web page in Python, utilize the RenderUrlAsPdf method. Simply provide the URL of the desired webpage as an argument to the method, as illustrated in the code snippet below: # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a URL or local file path pdf = renderer.RenderUrlAsPdf("https://ironpdf.com") # Export to a file or Stream pdf.SaveAs("url.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a URL or local file path pdf = renderer.RenderUrlAsPdf("https://ironpdf.com") # Export to a file or Stream pdf.SaveAs("url.pdf") PYTHON More information about converting Web Pages to PDFs is available on the URL to PDF Code Example page. Exploring PDF Formatting Options To customize the formatting of your PDF files, you can utilize the RenderingOptions attribute. This class provides various configurable settings to achieve the desired layout and appearance of your PDF documents. Some of the settings you can modify include page orientation, page size, margin size, and more. Set attributes available in RenderingOptions to generate PDF documents with the desired settings. Refer to this Code Example for more information about how to use the RenderingOptions. Secure PDF Files with Passwords To add password protection to PDF files, you can utilize the SecuritySettings attribute of the PdfDocument object. Begin by accessing the SecuritySettings attribute and assign a password to the UserPassword attribute, specified as a string. For instance, let's consider protecting the PDF document created in the "URL to PDF" example: # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") PYTHON The PDF file has been successfully password-protected. When attempting to open the file, a password prompt will be displayed. Simply enter the correct password to access the contents of the PDF file. Read more information about additional security and metadata settings. Complete Source Code The complete source file for this tutorial is included below: # Import statement for IronPDF for Python from ironpdf import * # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a HTML string using Python pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>") # Export to a file or Stream pdf.SaveAs("htmlstring_to_pdf.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an existing HTML file using Python pdf = renderer.RenderHtmlFileAsPdf("example.html") # Export to a file or Stream pdf.SaveAs("htmlfile_to_pdf.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a URL or local file path pdf = renderer.RenderUrlAsPdf("https://ironpdf.com") # Export to a file or Stream pdf.SaveAs("url.pdf") # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") # Import statement for IronPDF for Python from ironpdf import * # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a HTML string using Python pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>") # Export to a file or Stream pdf.SaveAs("htmlstring_to_pdf.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an existing HTML file using Python pdf = renderer.RenderHtmlFileAsPdf("example.html") # Export to a file or Stream pdf.SaveAs("htmlfile_to_pdf.pdf") # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from a URL or local file path pdf = renderer.RenderUrlAsPdf("https://ironpdf.com") # Export to a file or Stream pdf.SaveAs("url.pdf") # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") PYTHON IronPDF accurately renders all images and text while preserving their formatting. Interactive elements such as buttons remain clickable, and text boxes retain their editability within the generated PDF file. Summary In this How-To Guide, we explored the process of creating PDFs in Python using the IronPDF library. With IronPDF, developers can effortlessly generate and manipulate PDF documents. The library offers a user-friendly API that simplifies the creation of PDFs from various sources, including HTML files, XML documents, URLs, and more. Whether you're working on generating reports, invoices, or any other document type, IronPDF provides the necessary tools to accomplish the task efficiently. IronPDF is a commercial library and requires a valid license. It has a commercial license which starts from $749. To evaluate its capabilities in a production environment, you can take advantage of the free trial. Download the software product. Frequently Asked Questions How can I create PDF files from HTML using a Python library? You can use the RenderHtmlAsPdf method from the IronPDF library to convert HTML strings into PDF documents. This method allows the transformation of HTML content into high-quality PDFs efficiently. What are the steps to generate a PDF from a local HTML file in Python? With IronPDF, you can use the RenderHtmlFileAsPdf method to convert a local HTML file into a PDF. Simply provide the path to your HTML file as an argument to generate the PDF. Can IronPDF be used to convert webpages into PDF documents in Python? Yes, IronPDF allows you to create PDF documents from webpages using the RenderUrlAsPdf method. Input the URL of the webpage you wish to convert, and IronPDF will generate the corresponding PDF. What prerequisites are needed for using IronPDF in Python? To use IronPDF, ensure you have the .NET 6.0 SDK, Python 3.x, and pip installed on your machine. IronPDF integrates with .NET for its PDF generation capabilities. How do I customize the layout and appearance of PDFs using IronPDF? IronPDF provides a RenderingOptions attribute that allows you to customize PDF documents' layout and appearance, including options for page size, orientation, and margin size. Is it possible to secure PDF documents with passwords using IronPDF? Yes, IronPDF allows you to secure PDFs by setting a password. Access the SecuritySettings attribute of the PdfDocument object and set the UserPassword to secure your PDF. Do I need a license to use IronPDF in Python projects? IronPDF is a commercial library that requires a valid license key. A free trial is available for evaluation purposes, but a purchased license is needed to remove watermarks from generated PDFs. Where can I find more documentation and examples for using IronPDF? Detailed examples and comprehensive documentation for IronPDF can be found on the IronPDF official website, which includes guides and code snippets to assist in using the library effectively. How can I install IronPDF for creating PDFs in Python? IronPDF can be installed via pip, the Python package manager, using the command pip install ironpdf. Chaknith Bin Chat with engineering team now Software Engineer Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience. Ready to Get Started? Free pip Install View Licenses