I Made My Own Script To Fill Out Tedious PDF Forms For Me

I often need to fill out a PDF form for requesting medical records. I have typically done this using Preview since I can use it to digitally sign the form. But requesting new records after every appointment started becoming tedious–even when doing it digitally. I also did not want to wait for years and then request the records all at once (because it’s easier to handle large things like this via attrition rather than in bulk). So I made a script that would fill out PDF forms for me using the information I specified.

How It Works
This takes a bit of time to setup since the script enters text on the PDF at an x-y coordinate, but once done, I can fill out the form in a matter of seconds as opposed to minutes–very useful when I have several forms to fill out.

I manually edit the script below to contain most of the static information (my name, phone number, address, etc.) that is needed by the forms. Any thing else needed by the form is sent as an argument when running the script. So if I wanted to fill out a form and needed to put the dates of service I wanted the records for and the date I needed them by, I would run the script like this:
./requestMedicalRecords.py my_file.pdf "2016-06-03" "2016-06-09"
This would fill out the form with all my static information (already in the script) and then use the two dates above as that information can change with each form I fill out. This results in a new PDF with the same name but -filled appended to the file name.
Setting It Up
First I save the form with my signature applied to it since the script can’t do that (easily anyway).

Next is the tricky part–I had to get the x-y coordinates for each field I wanted to fill with text. The easiest method I found was to open the file in GIMP, which has a real-time coordinate tracker. You just need to import it in a certain way (see the screenshot below) for the coordinates to be accurate.

I noticed that some PDF forms that had actual text fields show up as little squares as shown in the screenshot below, so even though it looks weird, as long as you get the coordinates, it still works fine.
If you use the selector tool to drag up from the bottom left, the coordinates will be accurate. So I just needed to do this for each field I wanted to fill out, and then paired that with the appropriate text in the script.
can.drawString(143, 726, patient_name)

As an added bonus, I also save the form to my Dropbox, so that I can use Tiny Fax to digitally fax it right off to the correct place from my iPhone.
This trick is also part of a larger workflow I have for storing and maintaining my own medical records, which I will post about later.