Skip to content

Instantly share code, notes, and snippets.

@waylan
Last active January 11, 2024 19:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylan/36535feae946810bcdce5dfb8c6bdcf8 to your computer and use it in GitHub Desktop.
Save waylan/36535feae946810bcdce5dfb8c6bdcf8 to your computer and use it in GitHub Desktop.
A PDF fillable textfield which flows with text on a page using Reportlab.
from reportlab.platypus import SimpleDocTemplate, Flowable, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
style = getSampleStyleSheet()['BodyText']
class TextField(Flowable):
def __init__(self, **options):
Flowable.__init__(self)
self.options = options
# Use Reportlab's default size if not user provided
self.width = options.get('width', 120)
self.height = options.get('height', 36)
def draw(self):
self.canv.saveState()
form = self.canv.acroForm
form.textfieldRelative(**self.options)
self.canv.restoreState()
class ChoiceField(Flowable):
def __init__(self, **options):
Flowable.__init__(self)
options['relative'] = True
self.options = options
# Use Reportlab's default size if not user provided
self.width = options.get('width', 120)
self.height = options.get('height', 36)
def draw(self):
self.canv.saveState()
form = self.canv.acroForm
form.choice(**self.options)
self.canv.restoreState()
doc = SimpleDocTemplate('textfield.pdf')
Story = [
Paragraph('First Name', style=style),
TextField(name='first_name', value='John', tooltip='First name', height=18),
Paragraph('Last Name', style=style),
TextField(name='last_name', value='Doe', tooltip='Last name', height=18),
Paragraph('Gender', style=style),
ChoiceField(name='gender', tooltip='Gender', value='male', options=['', 'male', 'female'], height=18)
]
doc.build(Story)
@waylan
Copy link
Author

waylan commented Jun 17, 2021

I started by using this and this. This was also helpful in understanding how to use form fields, although it demonstrates absolute positioning them on the page (Reportlab's default behavior).

The resulting document (from the first version of this script) looks like this:

Untitled

@waylan
Copy link
Author

waylan commented Jun 23, 2021

I encountered two bugs when adding the ChoiceField.

  1. canvas.acroform.choiceRelative is a textfield, not a choice. Therefore, I had to use choice and manually set options['relative'] = True.

  2. A choice always fails if a value it not provided. Passing in an empty value does not fix the issue, even if an empty value is included in options. Therefore, a default (non-empty) value must always be provided. Unfortunately, this is not always desired. Sometimes we want to start out with a blank form field.

    Whenever, value='' is passed in I get the following error:

       File "textfield.py", line 36, in draw
         form.choice(**self.options)
       File "C:\code\md2pdf\venv\lib\site-packages\reportlab\pdfbase\acroform.py", line 1008, in choice
         return self._textfield(
       File "C:\code\md2pdf\venv\lib\site-packages\reportlab\pdfbase\acroform.py", line 844, in _textfield
         **lbextras
     UnboundLocalError: local variable 'lbextras' referenced before assignment
    

@karlb
Copy link

karlb commented Jun 1, 2022

Thanks, this has been very helpful!

@bthorben
Copy link

bthorben commented Sep 6, 2022

Did you manage to make the resulting boxes editable? I get such boxes (as a checkbox), but they are not editable (tried Preview.app and Adobe Acrobat Reader DC):
image

@bthorben
Copy link

bthorben commented Sep 6, 2022

BTW: In the way you are using it self.width and self.height is ignored. I created a text field (screenshot above of the generated PDF) for our purposes like this:

class TextFormField(Flowable):
    def __init__(self, tooltip, name, width=120, height=26):
        Flowable.__init__(self)
        self.tooltip = tooltip
        self.name = name
        self.width = width
        self.height = height

    def draw(self):
        self.canv.saveState()
        form = self.canv.acroForm
        form.textfieldRelative(
            name=self.name,
            tooltip=self.tooltip,
            width=self.width,
            height=self.height,
            borderStyle='inset',
            forceBorder=True
        )
        self.canv.restoreState()

@karlb
Copy link

karlb commented Sep 8, 2022

I didn't try checkboxes, so I can't comment on that. I also noticed that width and height are not properly forwarded to textfieldRelative in the original example. My implementation is

class TextField(Flowable):
    def __init__(self, **options):
        Flowable.__init__(self)
        options["width"] = options.get("width", 135)
        options["height"] = options.get("height", 12)
        options["fontSize"] = options.get("fontSize", 10)
        self.options = options

    def draw(self):
        self.canv.saveState()
        form = self.canv.acroForm
        form.textfieldRelative(**self.options)
        self.canv.restoreState()

@bthorben
Copy link

Okay, I figured out why this didn't work. It was actually a problem with the way we handle the pdf afterwards. We have a background PDF on which we put this form. We merge them with QPDF. QPDF confuses the fonts uses in the two pdf and creates a problem. It's okay, but not helpful that reportlab uses /F1 /F2 and so on for font names and doesn't let us change the prefix. We made a monkey-patch to change the prefix to ATF so fonts are /ATF1 /ATF2 and so on, this merges then correctly

@Ray-Mangan
Copy link

Ray-Mangan commented Sep 18, 2023

Thank you all for the above! FYI, I found that you can control the x position of the form item in the frame by using an indenter flowable which indents otherflowables after it's used. You add the indenter to the story before your flowables you want to indent, then add a indenter at the end with a negative value to dedent.

from reportlab.platypus import Indenter
indent18 = Indenter(left = 18)
dedent18 = Indenter(left = -18)

self.story = [
            p2,
            indent18,
            p2,
            q1,
            p2,
            dedent18,
            ]


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment