USING IRONPDF FOR PYTHON

How to Generate PDF Forms in Python

Updated August 24, 2023
Share:

1.0 Introduction

The Portable Document Format (PDF) file, developed by Adobe, is essential for maintaining the integrity of text-rich and aesthetically pleasing material when it comes to document sharing. It usually takes a specific program to access online PDF files and fill PDF forms. In the present day, many important digital publications need PDF files for creating interactive PDF forms. Many companies utilize PDF files to create professional documents and invoices using interactive elements such as radio buttons, listbox widgets, textfield widgets, scrollable boxes, and drop-down lists.

Developers commonly make use of libraries to create PDF documents to meet specific consumer requirements. Python can be used to extract specific text from existing PDF form fields by parsing it. IronPDF is a powerful Python library for processing PDF files, including extracting data, pictures, radio buttons, listbox widgets versus checkbox widgets, and other types of information. In this tutorial, we'll discover how to use this library to create PDF forms, create new PDF files, and explicitly group interactive forms with data.

Creating Interactive PDF Forms in Python

  1. Install the PDF library to generate PDF forms.
  2. Create a PDF form from an HTML string and save the PDF document.
  3. Upload the created PDF file using the PdfDocument class.
  4. Update the PDF document fields.
  5. Save the document to a new location.

2.0 IronPDF

Python is a programming language that aids in the quick and simple creation of graphical user interfaces. For programmers, Python is also far more dynamic than other languages. Because of this, adding the IronPDF library to Python is a simple process. A large range of pre-installed tools, such as PyQt, wxWidgets, Kivy, and many other packages and libraries, can be used to quickly and safely construct a fully complete GUI.

We can use IronPDF, which supports Python in addition to other programming languages, to combine a number of capabilities from other frameworks like .NET Core. For more information on IronPDF for Python, click here.

Python web design and development are made easier using IronPDF. As a result, three Python web development paradigms--Django, Flask, and Pyramid--have gained widespread recognition. Websites and online services such as Reddit, Mozilla, and Spotify have utilized these frameworks.

2.1 IronPDF Features

  • With IronPDF, PDFs can be produced from a variety of formats, including HTML, HTML5, ASPX, and Razor/MVC View. It offers the choice to convert images and HTML pages into PDF files.
  • Creating interactive PDFs, completing and submitting interactive forms, merging and dividing PDF files, extracting text and images, searching text within PDF files, rasterizing PDFs to images, changing font size, border and background color, and converting PDF files are all tasks that the IronPDF toolkit can help with.
  • IronPDF offers HTML login form validation with support for user-agents, proxies, cookies, HTTP headers, and form variables.
  • IronPDF uses usernames and passwords to allow users access to secured documents.
  • With just a few lines of code, we can print a PDF file from a variety of sources, including a string, stream, or URL.
  • IronPDF allows us to create flattened PDF documents.

3.0 Setup Python

3.1 Environment Configuration

Verify that Python is set up on your computer. To download and install the most recent version of Python that is compatible with your operating system, go to the official Python website. After installing Python, create a virtual environment to separate the requirements for your project. Your conversion project can have a tidy, independent workspace thanks to the venv module, which enables you to create and manage virtual environments.

3.2 New Project in PyCharm

For this article, we'll be using PyCharm, an IDE for developing Python code.

Once PyCharm IDE has started, choose "New Project".

How to Generate PDF Forms in Python: Figure 1 - PyCharm

When you choose "New Project," a new window will open where you can specify the environment and location of the project. You might find it easier to understand this in the image below.

How to Generate PDF Forms in Python: Figure 2 - New Project

After choosing the project location and environment path, click the "Create" button to launch a new project. The subsequent new window that appears can then be used to construct the software. Python 3.9 is used in this guide.

How to Generate PDF Forms in Python: Figure 3 - Create Project

3.3 IronPDF Library Requirement

Most of the time, the Python module IronPDF uses .NET 6.0. As a result, the .NET 6.0 runtime must be installed on your computer in order to use IronPDF with Python. It might be necessary to install .NET before this Python module can be used by Linux and Mac users. Visit this page to get the needed runtime environment.

3.4 IronPDF Library Setup

To generate, modify, and open files with the ".pdf" extension, the "ironpdf" package must be installed. Open a terminal window and enter the following command to install the package in PyCharm:

 pip install ironpdf

The installation of the 'ironpdf' package is shown in the screenshot below.

How to Generate PDF Forms in Python: Figure 4 - IronPDF

4.0 Generate PDF Forms

We can create interactive PDF Forms with the help of IronPDF. With a few lines of code, the sample PDF Form with a radio demo is shown below.

How to Generate PDF Forms in Python: Figure 5 - Sample PDF Form First Page

We are going to use the above HTML as a string to create PDF Forms in the following code.

from ironpdf import *
Form_Data = """
<html>
    <body>
        <table>
            <tr>
                <td>Name</td>
                <td><input name="name" type="text"/></td></td>
            </tr>
            <tr>
                <td>Age</td>
                <td><input name="age" type="text"/></td></td>
            </tr>
            <tr>
                <td>Gender</td>
            </tr>
            <tr>
                <td><input name="Gender" type="radio">Male</input></td>
                <td><input name="Gender" type="radio">Female</input></td>
            </tr>
        </table>
    </body>
</html>"""
# Step 1: Create PDF Form from HTML string
renderer = ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
renderer.RenderHtmlAsPdf(Form_Data).SaveAs("DemoForm.pdf")

# Step 2: Reading and Writing PDF form values
form_document = PdfDocument.FromFile("DemoForm.pdf")
name_field = form_document.Form.GetFieldByName("name")
name_field.Value = "Mike"
print("NameField value: {}".format(name_field.Value))
age_field = form_document.Form.GetFieldByName("age")
age_field.Value = "21"
print("AgeField value: {}".format(age_field.Value))
gender_field = form_document.Form.Fields [3]
gender_field.Value = "On"

form_document.SaveAs("UpdatedForm.pdf")
PYTHON

In the above example, first we create an object for the ChromePdfRenderer(), and then we set the RenderingOptions.CreatePdfFormsFromHtml value as true, which enables the forms in the PDF file. We use the RenderHtmlAsPdf method to pass the HTML string as a parameter.

With the help of the SaveAs parameter, we save the created PDF files from the HTML string. Now we have created a PDF Form from the HTML string, which will look like the image below and is initially empty. We use IronPDF again to update the fields in the PDF forms. In the PDF, the first two fields are text entry widgets and the other field is radio buttons.

How to Generate PDF Forms in Python: Figure 6 - PDF Form

Next, we use PdfDocument to upload the existing PDF Form that we generated earlier. With Form.GetFieldByName, we can get the PDF Form element by using the name of the element, or we can use Form.Fields [] by passing the element index into the array value. We can get and set the value of the element in the PDF Forms, and with the help of the SaveAs function, we can save the updated/new PDF file to a location.

How to Generate PDF Forms in Python: Figure 7 - Upload PDF Form

The above is the result from the code, which helps us fill PDF Forms. To learn more about PDF Forms, refer to the link here.

5.0 Conclusion

The IronPDF library offers strong security techniques to reduce risks and guarantee data security. It is not restricted to any particular browser and is compatible with all popular ones. With just a few lines of code, IronPDF enables programmers to quickly generate and read PDF files. The IronPDF library offers a range of licensing options to meet the diverse demands of developers, including a free developer license and additional development licenses that can be purchased.

The $749 Lite package includes a perpetual license, a 30-day money-back guarantee, a year of software maintenance, and upgrade options. There are no additional expenses after the initial purchase. These licenses can be used in development, staging, and production settings. Additionally, IronPDF provides free licenses with some time and redistribution restrictions. Users can assess the product in actual use during the free trial period without a watermark. Please click the following link to learn more about the cost of the IronPDF trial edition and how to license it.

< PREVIOUS
How to Generate PDF Forms in Python
NEXT >
How to Edit A PDF File in Python

Ready to get started? Version: 2024.7 just released

Free pip Install View Licenses >
123