How to Create PDF Files in Python
Create PDF files in Python using IronPDF 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 a PDF in Python
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/quickstart.py
# 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")// THIS CODE SNIPPET IS NOT AVAILABLE!
# 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")How to Create a PDF File in Python
- Install the IronPDF Python library
- Use the
RenderHtmlAsPdfmethod to convert an HTML string into a PDF document - Use the
RenderHtmlFileAsPdfmethod to generate a PDF file from a local HTML file - Use the
RenderUrlAsPdfmethod to create a PDF from a web page URL - Apply password protection and save the PDF to the desired directory
Automating PDF creation in Python lets developers generate documents programmatically: invoices, reports, contracts, and certificates, without leaving the application. Traditionally this required juggling layout engines and format specifications, but IronPDF's HTML-first approach means any HTML/CSS document becomes a pixel-accurate PDF in a single method call.
This How-To Guide covers the four main PDF creation workflows in IronPDF for Python: converting an HTML string, converting a local HTML file, converting a URL, and applying password protection before export.
What Python Library Should I Use for PDF Creation?
IronPDF is a Python library built specifically for creating PDF documents from HTML. Its API is straightforward: pass in HTML markup (or a file path, or a URL) and receive a PdfDocument object you can save, stamp, merge, or encrypt.
The library wraps IronPDF's battle-tested .NET rendering engine, the same engine used in .NET, Java, and Node.js projects, so the HTML-to-PDF output is consistent across platforms. It supports modern web standards including CSS3, HTML5, JavaScript execution, web fonts, and responsive layouts.
Beyond PDF generation, IronPDF covers the full PDF lifecycle:
- Text and data extraction from existing PDFs
- File format conversion (HTML, DOCX, images to PDF)
- Merging multiple PDFs
- Compressing PDF files
- Password encryption and digital rights management
- Filling PDF forms programmatically
What Prerequisites Do I Need Before Creating PDFs?
To use IronPDF for Python, the following software must be installed:
.NET 6.0 SDK: IronPDF for Python runs on top of the IronPDF .NET library. Download it from the official Microsoft .NET 6.0 download page.Python 3.x: Download and install Python from the official Python website. Select the option to add Python to PATH during installation.pip: Bundled with Python 3.4+. Verify installation by runningpip --versionin your terminal.IronPDF: Install the library using pip:
pip install ironpdf
pip3 instead of pip to install the Python 3 package. If you encounter installation errors, refer to our troubleshooting guide for OSError issues.What Code Setup Is Required Before Creating PDFs?
Add the following import statement to the top of your Python script:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/import.py
# Import statement for IronPDF for Python
from ironpdf import *// THIS CODE SNIPPET IS NOT AVAILABLE!
# Import statement for IronPDF for Python
from ironpdf import *Before any other IronPDF calls, configure a valid license key by assigning it to the LicenseKey attribute of License. This removes the trial watermark from generated PDFs. For detailed instructions on implementing license keys, visit the license key setup guide.
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/license.py
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"// THIS CODE SNIPPET IS NOT AVAILABLE!
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"How Do I Convert an HTML String into a PDF Document?
The RenderHtmlAsPdf method converts an HTML string directly into a PdfDocument object. Pass any valid HTML markup as the method argument and IronPDF renders it using its built-in Chromium engine, executing JavaScript and applying CSS before producing the PDF.
This approach is the most flexible for dynamically generated content: build the HTML string in Python (using template engines like Jinja2 or simple string formatting), then pass it directly to the renderer. Generating an invoice, for example, means populating an HTML template with order data and calling RenderHtmlAsPdf in one step.
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/html-string-to-pdf.py
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the Chromium renderer
renderer = ChromePdfRenderer()
# Convert an HTML string to a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the Chromium renderer
renderer = ChromePdfRenderer()
# Convert an HTML string to a PDF document
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 PdfDocument instance, call SaveAs with a target file path:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/save-html-string.py
# Save the PDF to a file
pdf.SaveAs("htmlstring_to_pdf.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
# Save the PDF to a file
pdf.SaveAs("htmlstring_to_pdf.pdf")The file htmlstring_to_pdf.pdf is written to the current working directory, preserving the HTML content with full CSS styling. For advanced HTML rendering techniques, including CSS frameworks, JavaScript frameworks, and complex layouts, see the HTML to PDF tutorial for Python.
How Do I Generate a PDF from a Local HTML File?
When the HTML content resides in a file on the local filesystem, RenderHtmlFileAsPdf is the right method. Provide the file path as the argument:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/html-file-to-pdf.py
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Create a PDF from a local HTML file
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Save the output PDF
pdf.SaveAs("htmlfile_to_pdf.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Create a PDF from a local HTML file
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Save the output PDF
pdf.SaveAs("htmlfile_to_pdf.pdf")IronPDF resolves relative paths for linked CSS files, images, and scripts relative to the HTML file's location. The renderer loads all associated assets before generating the PDF, so the result matches what a browser would display. This makes it a reliable option for templated documents where the HTML and assets are maintained as files on disk.
How Do I Create a PDF from a URL in Python?
Use RenderUrlAsPdf to convert a live web page to PDF. Provide the full URL as the argument:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/url-to-pdf.py
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Convert a web page to PDF
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Save the output PDF
pdf.SaveAs("url.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Convert a web page to PDF
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Save the output PDF
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, including JavaScript execution, before capturing the PDF, so all dynamic content is captured accurately.
Where Can I Find More URL Conversion Examples?
Additional examples for converting web pages to PDFs are available on the URL to PDF code example page. For web pages that require authentication, see the guide on website and system logins.
How Can I Customize PDF Formatting Options?
The RenderingOptions property of ChromePdfRenderer controls the formatting and layout of the output PDF. Common settings include page size, page orientation, margin size, and zoom level.
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/rendering-options.py
from ironpdf import *
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
renderer = ChromePdfRenderer()
# Configure rendering options
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait
renderer.RenderingOptions.MarginTop = 20
renderer.RenderingOptions.MarginBottom = 20
renderer.RenderingOptions.MarginLeft = 25
renderer.RenderingOptions.MarginRight = 25
# Render with custom formatting
pdf = renderer.RenderHtmlAsPdf("<h1>Custom Formatted PDF</h1><p>Content here.</p>")
pdf.SaveAs("formatted.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
renderer = ChromePdfRenderer()
# Configure rendering options
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait
renderer.RenderingOptions.MarginTop = 20
renderer.RenderingOptions.MarginBottom = 20
renderer.RenderingOptions.MarginLeft = 25
renderer.RenderingOptions.MarginRight = 25
# Render with custom formatting
pdf = renderer.RenderHtmlAsPdf("<h1>Custom Formatted PDF</h1><p>Content here.</p>")
pdf.SaveAs("formatted.pdf")What Formatting Settings Are Available?
The RenderingOptions property exposes settings for paper size, page orientation, margins, and zoom. The table below lists the most commonly used properties:
| Property | Type | Description |
|---|---|---|
PaperSize | PdfPaperSize | Standard page sizes (A4, Letter, Legal) or custom sizes |
PaperOrientation | PdfPaperOrientation | Portrait or Landscape page orientation |
MarginTop / MarginBottom | int (mm) | Top and bottom page margins in millimeters |
MarginLeft / MarginRight | int (mm) | Left and right page margins in millimeters |
HtmlHeader / HtmlFooter | HtmlHeaderFooter | Running HTML headers and footers with page number support |
For a complete reference with additional settings, see the PDF generation settings code example.
HtmlHeader and HtmlFooter properties on RenderingOptions. The headers and footers example demonstrates the full syntax.How Do I Secure PDF Files with Passwords?
To password-protect a PDF, set the UserPassword property on the SecuritySettings attribute of the PdfDocument object. An owner password can also be set to restrict editing permissions separately from read access.
Consider protecting the PDF created in the URL example:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/password-protect.py
from ironpdf import *
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Set user password (required to open the PDF)
pdf.SecuritySettings.UserPassword = "sharable"
# Set owner password (controls permissions)
pdf.SecuritySettings.OwnerPassword = "admin123"
# Configure document permissions
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Set user password (required to open the PDF)
pdf.SecuritySettings.UserPassword = "sharable"
# Set owner password (controls permissions)
pdf.SecuritySettings.OwnerPassword = "admin123"
# Configure document permissions
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")How Does Password Protection Work in Practice?
Opening protected.pdf in any PDF viewer prompts for the user password before displaying the document content. The owner password gives administrative access to modify the security settings. Setting AllowUserCopyPasteContent = False prevents readers from copying text out of the PDF while still permitting printing.
For further control over document permissions, see the security and metadata settings example and the PDF encryption and decryption guide.
What Is the Complete Source Code?
The combined example below demonstrates all four PDF creation workflows in a single Python script:
:path=/static-assets/pdf/content-code-examples/how-to/python-create-pdf/complete-example.py
from ironpdf import *
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# --- HTML string to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
pdf.SaveAs("htmlstring_to_pdf.pdf")
# --- Local HTML file to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlFileAsPdf("example.html")
pdf.SaveAs("htmlfile_to_pdf.pdf")
# --- URL to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
pdf.SaveAs("url.pdf")
# --- Password-protected PDF ---
pdf.SecuritySettings.UserPassword = "sharable"
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SaveAs("protected.pdf")// THIS CODE SNIPPET IS NOT AVAILABLE!
from ironpdf import *
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# --- HTML string to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
pdf.SaveAs("htmlstring_to_pdf.pdf")
# --- Local HTML file to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlFileAsPdf("example.html")
pdf.SaveAs("htmlfile_to_pdf.pdf")
# --- URL to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
pdf.SaveAs("url.pdf")
# --- Password-protected PDF ---
pdf.SecuritySettings.UserPassword = "sharable"
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SaveAs("protected.pdf")IronPDF renders all images and text accurately while preserving formatting. Interactive elements such as buttons remain functional, and text boxes retain their editability within the generated PDF.
What Are the Next Steps for Creating PDFs in Python?
This guide covered the three primary PDF creation methods in IronPDF for Python — HTML string, local HTML file, and URL — along with password protection for securing output files. Each method produces a PdfDocument object that can be further processed: stamped, merged, split, or encrypted before saving.
Ready to extend these workflows? The following resources demonstrate practical next steps:
- HTML to PDF Python Tutorial — advanced rendering with CSS frameworks and JavaScript
- Merge PDFs in Python — combine multiple documents into one file
- Compress PDFs in Python — reduce file size for storage and email
- Fill PDF Forms in Python — populate existing PDF form fields
IronPDF requires a valid license for production use. Licensing starts from $999. Start a free 30-day trial to evaluate the library without restrictions, or view licensing options for the full product suite.
Frequently Asked Questions
How do I install IronPDF for Python?
Run pip install ironpdf in your terminal. You also need Python 3.x and the .NET 6.0 SDK installed, since IronPDF for Python runs on top of the IronPDF .NET engine.
What is the simplest way to convert HTML to PDF in Python?
Create a ChromePdfRenderer instance, then call renderer.RenderHtmlAsPdf('<h1>Hello</h1>'). The method accepts any valid HTML string and returns a PdfDocument object you can save with pdf.SaveAs('output.pdf').
Can I generate a PDF from a local HTML file?
Yes. Use renderer.RenderHtmlFileAsPdf('path/to/file.html'). IronPDF resolves linked CSS, images, and scripts relative to the HTML file location before rendering.
How do I convert a live web page URL to PDF?
Call renderer.RenderUrlAsPdf('https://example.com'). IronPDF waits for the full page to load, including JavaScript execution, before capturing the PDF.
How do I password-protect a generated PDF?
Set pdf.SecuritySettings.UserPassword to require a password to open the file, and pdf.SecuritySettings.OwnerPassword to restrict editing permissions. Call pdf.SaveAs afterwards to write the protected file.
Can I control the page size and margins of generated PDFs?
Yes. Configure renderer.RenderingOptions.PaperSize (e.g. PdfPaperSize.A4) and set MarginTop, MarginBottom, MarginLeft, and MarginRight in millimeters before calling a render method.
Does IronPDF support JavaScript execution in HTML to PDF conversion?
Yes. IronPDF uses a Chromium-based rendering engine that executes JavaScript, processes AJAX calls, and applies CSS before generating the PDF, so dynamic content is captured accurately.
Can I merge or compress PDFs after creating them?
Yes. IronPDF provides separate methods for merging multiple PdfDocument objects into one file and for compressing existing PDFs to reduce file size. Both operations work on the PdfDocument object returned by any render method.







