Creating a Form in a PDF Document in Python With borb

Creating a Form in a PDF Document in Python With borb

The Portable Document Format (PDF) is not a WYSIWYG (What You See is What You Get) format. It was developed to be platform-agnostic, independent of the underlying operating system and rendering engines.

To achieve this, PDF was constructed to be interacted with via something more like a programming language, and relies on a series of instructions and operations to achieve a result. In fact, PDF is based on a scripting language - PostScript, which was the first device-independent Page Description Language.

In this guide, we'll be using borb - a Python library dedicated to reading, manipulating and generating PDF documents. It offers both a low-level model (allowing you access to the exact coordinates and layout if you choose to use those) and a high-level model (where you can delegate the precise calculations of margins, positions, etc to a layout manager).

In this guide, we'll take a look at how to generate a PDF with a fillable form.

Installing borb

borb can be downloaded from source on GitHub, or installed via pip:

$ pip install borb

Generating a PDF Document with borb

Now that borb is installed, we can import the building blocks and construct a simple PDF page:

from borb.pdf.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF
from borb.pdf.canvas.layout.page_layout.multi_column_layout import SingleColumnLayout
from borb.pdf.canvas.layout.page_layout.page_layout import PageLayout

The following code represents the basic steps on creating a PDF document using borb:

  • Creating an empty Document
  • Creating an empty Page
  • Appending the Page to the Document
  • Creating a PageLayout that is responsible for handling the flow of content (here we'll use SingleColumnLayout)
  • Adding content to the PageLayout
  • Persisting the Document to disk

With that being said, let's go ahead and create a Document:

# Create empty Document
pdf = Document()

# Create empty Page
page = Page()

# Add Page to Document
pdf.append_page(page)

# Create PageLayout
layout: PageLayout = SingleColumnLayout(page)

With the initial steps out of the way - we can add the content in. In this instance, it'll be a fillable form. We're going to create a form with some basic user-information questions, such as a name, surname, etc:

  • Name
  • Surname
  • Gender
  • Place of residence
  • Nationality

To ensure everything is laid out just right, we're going to add this content to a Table. The left column will contain the field name (e.g. "name", "surname"), the right column will contain the fields to be filled in.

We'll additionally add another Paragraph right above the form to annotate it:

# New import(s)
from borb.pdf.canvas.layout.table.fixed_column_width_table import FixedColumnWidthTable
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.canvas.layout.forms.text_field import TextField
from borb.pdf.canvas.color.color import HexColor
from decimal import Decimal
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.forms.drop_down_list import DropDownList

# Let's start by adding a heading
layout.add(Paragraph("Patient Information:", font="Helvetica-Bold"))

# Use a table to lay out the form
table: FixedColumnWidthTable = FixedColumnWidthTable(number_of_rows=5, number_of_columns=2)

# Name
table.add(Paragraph("Name : ", horizontal_alignment=Alignment.RIGHT, font_color=HexColor("56cbf9")))
table.add(TextField(value="Doe", font_color=HexColor("56cbf9"), font_size=Decimal(20)))

# Surname
table.add(Paragraph("Surname : ", horizontal_alignment=Alignment.RIGHT, font_color=HexColor("56cbf9")))
table.add(TextField(value="John", font_color=HexColor("56cbf9"), font_size=Decimal(20)))

These inpt fields are TextFields, which accept a string passed into them. We're going to model the gender field as a dropdown list, from which the reader can choose one of four options:

  • Female
  • Male
  • Other
  • Prefer not to disclose

Let's see how that translates to borb:

# Gender
table.add(Paragraph("Gender : ", horizontal_alignment=Alignment.RIGHT))
table.add(DropDownList(
    possible_values=[
                    "Female",
                    "Male",
                    "Other",
                    "Prefer not to disclose",
                    ]
))

We could do a similar thing for country of residence and nationality, but it would involve having to find a list of all countries in the world and passing to the constructor of DropDownList.

This stands for any sufficiently long list.

Because this particular field (a list of all countries) is such a common requirement, borb comes pre-loaded with the class CountryDropDownList:

# New import(s)
from borb.pdf.canvas.layout.forms.country_drop_down_list import CountryDropDownList

# Country of Residence
table.add(Paragraph("Country of Residence : ", horizontal_alignment=Alignment.RIGHT))
table.add(CountryDropDownList(value="Belgium"))

# Nationality
table.add(Paragraph("Nationality : ", horizontal_alignment=Alignment.RIGHT))
table.add(CountryDropDownList(value="Belgium"))

Now we can finally add the Table to our PageLayout:

# Set some properties on the table to make the layout prettier
table.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5))
table.no_borders()

# Adding Table to PageLayout
layout.add(table)

Now let's add a (nonsense) data protection policy:

# Data protection policy
layout.add(Paragraph("Data Protection Policy", 
                     font="Helvetica-Bold"))

# Dummy text
layout.add(Paragraph(
    """
    ** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    """,
    font="Helvetica-Oblique"
))

Let's wrap things up by adding a footer. For now, we'll just add a rectangle filled in the accent color, at the bottom of the page. Nothing too fancy.

# New import(s)
import typing
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.page.page_size import PageSize
from borb.pdf.canvas.line_art.line_art_factory import LineArtFactory
from borb.pdf.canvas.layout.image.shape import Shape

ps: typing.Tuple[Decimal, Decimal] = PageSize.A4_PORTRAIT.value
r: Rectangle = Rectangle(Decimal(0), Decimal(32), ps[0], Decimal(8))
Shape(points=LineArtFactory.rectangle(r), stroke_color=HexColor("56cbf9"), fill_color=HexColor("56cbf9")).layout(page, r)

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Lastly, we can store the Document we created using the PDF class:

# New import(s)
from borb.pdf.pdf import PDF

# Store
with open("output.pdf", "wb") as out_file_handle:
    PDF.dumps(out_file_handle, pdf)

How does this look like in the end? When we run the code and produce the PDF file - it'll have a few empty fields:

By selecting these fields, you can use your keyboard to enter the details in:

Conclusion

In this guide you've learned how to include form-elements in your PDF, allowing the reader to interact with the PDF.

Last Updated: October 20th, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Joris SchellekensAuthor

I'm a software architect from Belgium, with a passion for machine learning, knowledge-based systems and graph algorithms. I'm also the author of borb, the pure python PDF library.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Make Clarity from Data - Quickly Learn Data Visualization with Python

    Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

    From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

    © 2013-2021 Stack Abuse. All rights reserved.