Recently, we had the opportunity to work on a very simple yet challenging task. The problem statement was to create a PDF file programmatically and to calculate some values dynamically while filling up the AcroForm of a PDF file.
The purpose of writing this article is to share the challenges faced and what we did to overcome them. In this article, we’ll consider a simple single-page PDF file with 3 AcroForm fields where the 3rd field gets populated by the multiplication of the other 2 fields.
You can make use of the canvas to create the AcroForms within it. Reportlab provides the canvas from its pdfgen utility.
Below you’ll find sample code to create this simple pdf file.
For the sake of simplicity, we will create a basic AcroForm PDF, which will contain 3 input fields. This function would create the pdf file with the current timestamp and return the file name while saving it.
In this small snippet, it would take the input values from the textfield with name ‘price’ and ‘quantity’ and set out the value of textfield with name ‘total’ in the pdf file.
To create this in python, we will use the library of pdfrw, which supports the PDF object model.
We are good to go in generating the PDF file now.
This will create the PDF file in 2 separate process.
When we started working on the project, the documentations or articles related to the project were very hard to find. We couldn’t find a single place that could conclusively provide the core solution i.e the triggering of the JS. We couldn’t take no for a solution so we dug deep into the PDF documentations which eventually led us to believe in our ethos more than ever. We at TheCodeWork get things done. If you have some interesting ideas or a challenging problem, connect with us to bring it to life.
Our Content Team at TheCodeWork believes in quality content. We write everything related to startups and products at large. We publish our blog every Wednesday. Subscribe to our newsletter to get notified on our awesome content.