How to Create PDF Files in Python

In this tutorial, we explore how to create PDF files using Iron PDF in Python. After installing the Iron PDF project, we start by importing necessary namespaces and setting the license key to access all features. We then demonstrate creating PDFs from simple HTML strings by instantiating a Chrome PDF renderer object and using the render_HTML_as_PDF method to generate a PDF saved as HTML_string2pdf.pdf. Additionally, we cover creating PDFs from existing HTML files and URLs, which highlights Iron PDF's capability to accurately convert full HTML content and live web pages into PDFs, preserving layout and formatting. Furthermore, we illustrate how to enhance PDF security by setting a user password, saving the protected document as protected.pdf. This feature ensures that only authorized users with the password can access sensitive information within the PDF. Overall, the tutorial provides a comprehensive guide on utilizing Iron PDF to effectively create and manage PDFs in Python, catering to various needs such as archiving web content or sharing consistent document formats. Users are encouraged to like, subscribe, and explore the software further by downloading it from the provided link.

Here’s a sample code, illustrating these concepts:

# Import necessary namespaces from the IronPDF package
from ironpdf import *

# Set the license key for IronPDF (Make sure to replace 'your-license-key' with your actual key)
license_key = 'your-license-key'

# Create a PDF from an HTML string
html_content = "<h1>Hello, World!</h1><p>This is a PDF created from an HTML string.</p>"
# Instantiate a Chrome PDF renderer
renderer = ChromePdfRenderer()

# Render the HTML to PDF
pdf_document = renderer.render_html_as_pdf(html_content)

# Save the rendered PDF to a file
pdf_document.save_as('HTML_string2pdf.pdf')

# Create a PDF from an existing HTML file
html_file_path = 'example.html'  # Path to your local HTML file
pdf_document_file = renderer.render_html_file_as_pdf(html_file_path)

# Save the rendered PDF to a file
pdf_document_file.save_as('HTML_file2pdf.pdf')

# Create a PDF from a URL
url = 'https://www.example.com'
pdf_document_url = renderer.render_url_as_pdf(url)

# Save the rendered PDF to a file
pdf_document_url.save_as('URL2pdf.pdf')

# Enhance PDF security by setting a user password
password_protected_pdf = pdf_document.protect(password='securepassword')

# Save the password-protected PDF
password_protected_pdf.save_as('protected.pdf')
# Import necessary namespaces from the IronPDF package
from ironpdf import *

# Set the license key for IronPDF (Make sure to replace 'your-license-key' with your actual key)
license_key = 'your-license-key'

# Create a PDF from an HTML string
html_content = "<h1>Hello, World!</h1><p>This is a PDF created from an HTML string.</p>"
# Instantiate a Chrome PDF renderer
renderer = ChromePdfRenderer()

# Render the HTML to PDF
pdf_document = renderer.render_html_as_pdf(html_content)

# Save the rendered PDF to a file
pdf_document.save_as('HTML_string2pdf.pdf')

# Create a PDF from an existing HTML file
html_file_path = 'example.html'  # Path to your local HTML file
pdf_document_file = renderer.render_html_file_as_pdf(html_file_path)

# Save the rendered PDF to a file
pdf_document_file.save_as('HTML_file2pdf.pdf')

# Create a PDF from a URL
url = 'https://www.example.com'
pdf_document_url = renderer.render_url_as_pdf(url)

# Save the rendered PDF to a file
pdf_document_url.save_as('URL2pdf.pdf')

# Enhance PDF security by setting a user password
password_protected_pdf = pdf_document.protect(password='securepassword')

# Save the password-protected PDF
password_protected_pdf.save_as('protected.pdf')
PYTHON

Further Reading: How to Create PDF Files in Python

Chaknith Bin
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.
< PREVIOUS
How to Merge PDF Files Using Python
NEXT >
How to Compress PDF Files Using Python

Ready to get started? Version: 2025.6 just released

View Licenses >