Hello all,
I recently had the challenge of populating a fillable PDF with data collected from an Anvil app.
There is a great python library installed on the Anvil servers now called pdfrw which makes this process fairly easy.
Here’s how:
-
Fill the editable fields in your PDF with placeholders like so:
-
Save the PDF as media in a DataTable. It will be used as a template.
-
On the Anvil server, use the pdfrw library to fill the PDF’s fields based on a dictionary that has keys equal to your placeholders and values equal to your form data (or data from your DataTables). For example:
data_dict={'customer_name': self.text_box.name.text, 'description': self.text_box.description.text, 'total': self.text_box.total.text}
-
The final PDF will be a copy of your template that is filled out according to the values in your dictionary
Here it is in action:
Here is a clone:
https://anvil.works/build#clone:KWUTNAW2T4OG7MVH=GAMTG7KTEIWLV2QEX47YZRL7
Working with PDFs in code is generally a nightmare but pdfrw worked quite well, even with a multi-page PDF containing dozens of fillable fields.