-
-
Save waylan/36535feae946810bcdce5dfb8c6bdcf8 to your computer and use it in GitHub Desktop.
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) |
I encountered two bugs when adding the ChoiceField
.
-
canvas.acroform.choiceRelative
is atextfield
, not achoice
. Therefore, I had to usechoice
and manually setoptions['relative'] = True
. -
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 inoptions
. 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
Thanks, this has been very helpful!
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()
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()
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
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,
]
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: