How to Create PDF Files in Python
Create PDF files in Python using IronPDF library by converting HTML strings, HTML files, or URLs to PDF documents with just a few lines of code. IronPDF handles rendering, formatting, and security features automatically.
Quickstart: Create PDF in Python
:title=Quickstart
# 1. Install IronPDF: pip install ironpdf
# 2. Import the library
from ironpdf import *
# 3. Create renderer
renderer = ChromePdfRenderer()
# 4. Convert HTML to PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
# 5. Save the PDF
pdf.SaveAs("output.pdf"):title=Quickstart
# 1. Install IronPDF: pip install ironpdf
# 2. Import the library
from ironpdf import *
# 3. Create renderer
renderer = ChromePdfRenderer()
# 4. Convert HTML to PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
# 5. Save the PDF
pdf.SaveAs("output.pdf")Automating PDF creation in Python lets developers generate PDFs within their applications. This capability proves useful for generating invoices, reports, or other document types 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
RenderHtmlAsPdfmethod to transform an HTML string into a PDF document - Use the
RenderHtmlFileAsPdfmethod to generate a PDF file directly from an HTML file - Leverage the
RenderUrlAsPdfmethod to create PDF files from URL - Export password-protected PDF Files to desired directory
What Is IronPDF Python PDF Library?
IronPDF is a Python library designed for creating PDF documents from HTML. Its APIs make it straightforward to generate and customize PDFs with various features, including:
- Adding text, images, and other content types
- Choosing fonts, colors, and controlling document layout and formatting.
IronPDF can be integrated into .NET, Java, and Python applications, enabling PDF generation across multiple platforms.
Beyond PDF generation, IronPDF offers additional features. These include file format conversion, text and data extraction from PDFs, and the ability to secure PDFs through password encryption. For advanced use cases, explore how to merge multiple PDFs, compress PDF files, or fill PDF forms programmatically.
What Are the Steps to Create PDF Document in a Python Script?
What Prerequisites Do I Need?
To use IronPDF for Python, ensure your computer has the following software installed:
.NET 6.0 SDK: Required as IronPDF Python relies on the IronPDF .NET library. Download from the official Microsoft website.Python: Download and install Python 3.x from https://www.python.org/downloads/. Select the option to add Python to PATH during installation.Pip: Usually bundled with Python 3.4+. Verify installation or install separately if needed.IronPDF Library: Install using pip with the command below:
pip install ironpdf
What Code Setup Is Required Before Creating PDFs?
First, add the statement below to the top of your Python script:
# Import statement for IronPDF for Python
from ironpdf import *# Import statement for IronPDF for Python
from ironpdf import *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). For detailed instructions on implementing license keys in your project, visit our license key setup guide.
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"How Do I Convert HTML String into PDF Document?
What Method Should I Use for HTML String Conversion?
Use the RenderHtmlAsPdf method to generate a new PDF document from an HTML string. This method supports CSS styling, JavaScript execution, and custom fonts.
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>")How Do I Save the Generated PDF?
Once the HTML string has been 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")A PDF file named "htmlstring_to_pdf.pdf" will be created, preserving the contents of the original HTML string. For more advanced HTML rendering techniques, including working with complex layouts and JavaScript frameworks, see our comprehensive HTML to PDF tutorial.
How Do I Generate PDF from HTML file in Python?
What Is the Process for Converting Local HTML Files?
To generate a PDF document from an HTML file stored locally in Python, follow the code below. The RenderHtmlFileAsPdf method provides a direct way to convert existing HTML documents:
# 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")Why Does IronPDF Preserve HTML Formatting?
In the code above, the RenderHtmlFileAsPdf method creates a PDF document from an HTML file. 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. The library supports modern web standards including CSS3, HTML5, and JavaScript frameworks, making it ideal for converting complex web documents to PDF format.
Use the SaveAs method to save the generated PDF to a specific location on your system, similar to the previous example.
How Do I Create PDF from URL in Python?
What Method Converts Web Pages to PDF?
To create a PDF document from a web page in Python, use the RenderUrlAsPdf method. Provide the URL of the desired webpage as an argument to the method:
# 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")The URL rendering feature supports modern web technologies, including dynamic JavaScript content, AJAX calls, and responsive layouts. IronPDF waits for the page to fully load before converting, ensuring all content is captured accurately.
Where Can I Find More URL Conversion Examples?
More information about converting Web Pages to PDFs is available on the URL to PDF Code Example page. For websites requiring authentication, explore our guide on website and system logins.
How Can I Customize PDF Formatting Options?
What Settings Can I Modify in RenderingOptions?
To customize the formatting of your PDF files, use the RenderingOptions attribute. This class provides various configurable settings to achieve the desired layout and appearance of your PDF documents. Settings 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.
Additional customization options include:
- Setting custom page margins
- Configuring portrait and landscape orientations
- Defining custom PDF page sizes
- Managing page numbers and page breaks
How Do I Secure PDF Files with Passwords?
What Is the Process for Adding Password Protection?
To add password protection to PDF files, use the SecuritySettings attribute of the PdfDocument object. Access the SecuritySettings attribute and assign a password to the UserPassword attribute, specified as a string. For more advanced security features, including encryption and digital signatures, visit our security and metadata example page.
For instance, consider protecting the PDF document created in the "URL to PDF" example:
# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"
# Configure additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"
# Configure additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")How Does Password Protection Work in Practice?
The PDF file has been password-protected. When attempting to open the file, a password prompt will appear. Enter the correct password to access the contents of the PDF file. The owner password provides additional administrative privileges, allowing you to modify security settings later.
Read more information about additional security and metadata settings and explore PDF encryption and decryption capabilities.
What Is the 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")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.
What Are the Key Takeaways?
In this How-To Guide, we explored the process of creating PDFs in Python using the IronPDF library. With IronPDF, developers can generate and manipulate PDF documents efficiently.
The library offers an API that simplifies the creation of PDFs from various sources, including HTML files, XML documents, URLs, and more. Whether you're generating reports, invoices, or any other document type, IronPDF provides the necessary tools to accomplish the task.
IronPDF is a commercial library and requires a valid license. It has a commercial license which starts from $799. To evaluate its capabilities in a production environment, you can take advantage of the free trial license key.
Frequently Asked Questions
How do I install the Python library for creating PDF files?
Install IronPDF using pip with the command: pip install ironpdf. Make sure you have Python 3.x and .NET 6.0 SDK installed on your system first, as IronPDF Python relies on the IronPDF .NET library.
What is the simplest way to convert HTML to PDF in Python?
The simplest way is using IronPDF's RenderHtmlAsPdf method. Just import IronPDF, create a ChromePdfRenderer instance, and use renderer.RenderHtmlAsPdf('Your HTML content') to convert HTML strings directly to PDF documents.
Can I create PDFs from existing HTML files?
Yes, IronPDF provides the RenderHtmlFileAsPdf method that allows you to generate PDF files directly from HTML files stored on your system, maintaining all formatting and styles.
How can I convert a webpage URL to PDF in Python?
Use IronPDF's RenderUrlAsPdf method to create PDF files from any URL. The library will render the webpage exactly as it appears in a browser and convert it to a PDF document.
What prerequisites do I need to create PDFs in Python?
You need Python 3.x, pip package manager, .NET 6.0 SDK (since IronPDF Python relies on the .NET library), and the IronPDF library installed via pip.
Can I add security features to my generated PDFs?
Yes, IronPDF allows you to export password-protected PDF files and secure your documents through password encryption, ensuring your generated PDFs are protected.
What types of content can I add to PDFs?
With IronPDF, you can add text, images, and other content types to your PDFs. You can also customize fonts, colors, and control the document layout and formatting.
Is it possible to merge or compress PDF files programmatically?
Yes, IronPDF offers advanced features beyond basic PDF generation, including the ability to merge multiple PDFs, compress PDF files, and fill PDF forms programmatically.







