Fill a PDF with Anvil (and pdfrw)

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.

10 Likes

Well, that’s awesome!

1 Like

This is great! It can be used as a feature to download data generated by any application, definitely using it

1 Like

This is great! Thank you!

1 Like

Anvil now has a built-in PDF creator! You can output any Form as a PDF.

The PDF is a Media object so it can be downloaded (and then printed by the user), as well as emailed, and stored in a Data Table.

1 Like