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)
@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