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.
How to Fill PDF Form in Python
- Install the Python library required for filling PDF forms
- Load an existing PDF document that contains form fields
- Access the desired form field using the
GetFieldByName
method of the Form attribute - Populate the form field by assigning a value to the Value attribute
- Export the modified PDF document
IronPDF — a Python Library
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 document, 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 *
# Create a PDF with editable forms from HTML using form and input tags
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 Renderer
renderer = ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
renderer.RenderHtmlAsPdf(form_html).SaveAs("BasicForm.pdf")
# Read and Write PDF form values
form_document = PdfDocument.FromFile("BasicForm.pdf")
# Set and Read the value of the "firstname" field
first_name_field = form_document.Form.GetFieldByName("firstname")
first_name_field.Value = "Minnie"
print("FirstNameField value: {}".format(first_name_field.Value))
# Set and Read the value of the "lastname" field
last_name_field = form_document.Form.GetFieldByName("lastname")
last_name_field.Value = "Mouse"
print("LastNameField value: {}".format(last_name_field.Value))
form_document.SaveAs("FilledForm.pdf")
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
Secondly, we load the created PDF utilizing the PdfDocument.FromFile
method. By utilizing the GetFieldByName
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
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.