How to Fill PDF Form in Python (Tutorial)

Instead of manually filling PDF fields one by one, we will focus on programmatically filling PDFs in this article. This approach becomes useful when an application's user interface enhances the user experience, but the PDF files need to be generated electronically for archiving purposes.

Once all the user input data has been collected, it becomes necessary to programmatically create PDF forms. These filled-out documents can then be saved for future use or modified as needed. There are several Python PDF libraries available for working with PDFs, including PyPDF2, ReportLab, IronPDF, and more. In this article, we will specifically explore how to use IronPDF for filling interactive forms.

Introduction to IronPDF for Python

IronPDF is a powerful PDF library designed for Python developers, providing them with a straightforward approach to creating, editing, and manipulating PDF documents within their Python scripts.

Developers can take advantage of IronPDF's comprehensive feature set, which includes capabilities for manipulating text and images, encrypting documents, and implementing digital signatures. By utilizing IronPDF, developers can efficiently generate top-notch PDF documents, thereby enhancing the value and efficiency of their Python projects.

Install IronPDF via Pip

The IronPDF library can be added via pip. Use the command below to install IronPDF using pip:

 pip install ironpdf

Now you can use IronPDF with your Python script.

Using Python Code to Fill PDF Documents Programmatically

Below is an illustrative code snippet showcasing the utilization of the IronPDF library to create and populate PDF forms using HTML markup. The provided code imports the essential classes from the IronPDF library.

from ironpdf import *

# Define HTML content for a simple form
form_html = """
<html>
<body>
<h2>Editable PDF Form</h2>
<form>
First name: <br> <input type='text' name='firstname' value=''> <br>
Last name: <br> <input type='text' name='lastname' value=''>
</form>
</body>
</html>
"""

# Instantiate a PDF renderer
renderer = ChromePdfRenderer()

# Set the option to create PDF forms from HTML
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

# Render the HTML content as a PDF file and save it
renderer.RenderHtmlAsPdf(form_html).SaveAs("BasicForm.pdf")

# Load the created PDF document
form_document = PdfDocument.FromFile("BasicForm.pdf")

# Access the "firstname" field and set its value
first_name_field = form_document.Form.FindFormField("firstname")
first_name_field.Value = "Minnie"
print("FirstNameField value: {}".format(first_name_field.Value))

# Access the "lastname" field and set its value
last_name_field = form_document.Form.FindFormField("lastname")
last_name_field.Value = "Mouse"
print("LastNameField value: {}".format(last_name_field.Value))

# Save the filled form to a new PDF file
form_document.SaveAs("FilledForm.pdf")
from ironpdf import *

# Define HTML content for a simple form
form_html = """
<html>
<body>
<h2>Editable PDF Form</h2>
<form>
First name: <br> <input type='text' name='firstname' value=''> <br>
Last name: <br> <input type='text' name='lastname' value=''>
</form>
</body>
</html>
"""

# Instantiate a PDF renderer
renderer = ChromePdfRenderer()

# Set the option to create PDF forms from HTML
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

# Render the HTML content as a PDF file and save it
renderer.RenderHtmlAsPdf(form_html).SaveAs("BasicForm.pdf")

# Load the created PDF document
form_document = PdfDocument.FromFile("BasicForm.pdf")

# Access the "firstname" field and set its value
first_name_field = form_document.Form.FindFormField("firstname")
first_name_field.Value = "Minnie"
print("FirstNameField value: {}".format(first_name_field.Value))

# Access the "lastname" field and set its value
last_name_field = form_document.Form.FindFormField("lastname")
last_name_field.Value = "Mouse"
print("LastNameField value: {}".format(last_name_field.Value))

# Save the filled form to a new PDF file
form_document.SaveAs("FilledForm.pdf")
PYTHON

Firstly, we generate a PDF form by converting an HTML form marked up using the PdfDocument.RenderHtmlAsPdf method. The RenderingOptions attribute is used to set the CreatePdfFormsFromHtml to true, which makes the form within the HTML markup editable. The resulting PDF is then saved to the specified output location using the SaveAs method.

Output

Initial PDF form output

Secondly, we load the created PDF utilizing the PdfDocument.FromFile method. By utilizing the FindFormField method, we access the specified form fields based on their respective names. To populate the firstname and lastname input fields, we assign values to their Value attributes. Finally, the modified PDF is saved to a new file using the SaveAs method.

Output

Filled PDF form output

Summary

In conclusion, IronPDF stands out as a dependable and efficient Python library for PDF document manipulation. With its exceptional capability to programmatically fill PDF forms, it becomes an invaluable asset in automating document processing workflows.

IronPDF provides a free trial, allowing users to explore its features before committing. Moreover, it offers licensing options that are both affordable and flexible, with packages starting at $749.

Frequently Asked Questions

What is IronPDF?

IronPDF is a powerful PDF library designed for Python developers, providing a straightforward approach to creating, editing, and manipulating PDF documents within Python scripts.

How do I install IronPDF in Python?

You can install IronPDF via pip using the command: pip install ironpdf.

How can I fill PDF forms programmatically using IronPDF?

To fill PDF forms programmatically using IronPDF, load an existing PDF with form fields, access the desired fields using the FindFormField method, set their values, and save the modified document.

What are some features of IronPDF?

IronPDF offers features like text and image manipulation, document encryption, and digital signature implementation, making it a comprehensive tool for PDF manipulation.

Can IronPDF convert HTML forms into PDF forms?

Yes, IronPDF can convert HTML forms into PDF forms using the PdfDocument.RenderHtmlAsPdf method with the CreatePdfFormsFromHtml option enabled.

Is there a trial version of IronPDF available?

Yes, IronPDF offers a free trial allowing users to explore its features before committing to a purchase.

What are the licensing options for IronPDF?

IronPDF provides affordable and flexible licensing options, with packages starting at a liteLicense.

How do you save a filled PDF form using IronPDF?

After filling the form fields, you can save the filled PDF document using the SaveAs method of the PdfDocument class.

What is the role of the ChromePdfRenderer in IronPDF?

The ChromePdfRenderer in IronPDF is used to render HTML content into a PDF file, allowing for the creation of PDF forms from HTML markup.

How does IronPDF enhance Python projects?

IronPDF enhances Python projects by providing efficient, high-quality PDF document generation and manipulation capabilities, thereby automating and streamlining document processing workflows.

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.