emperor: (Default)
posted by [personal profile] emperor at 06:46pm on 15/05/2018 under ,
I needed to fill in a PDF form recently (the proper editable sort, not a flat PDF with empty spaces); me being me I wanted to use Free tools, and ideally do so in a way that I could use version control on the bits I was adding to the form.

Thankfully, with pdftk, this is pretty doable. It was mostly only a bit fiddly because there wasn't a coherent answer to some of the edge cases (e.g. round tick boxes). Basically, the process is:


  1. pdftk generate_fdf to get the file you want to edit
  2. pdftk dump_data_fields if necessary to learn about tickboxes
  3. edit the fdf file
  4. pdftk fill_form to fill the form in



As an example form, here's a publically-available one. If you run pdftk n1-eng.pdf generate_fdf output fields.fdf, you get an .fdf (Form Data Format) file. It looks a bit weird, but basically you can edit the relevant bits of contents as text.

An excerpt:
<<
/V ()
/T (In the)
>> 
<<
/V /
/T (Check Box34)
>> 


So if you put some text in the parentheses above "In the", then that text appears in the relevant box on the form. In this case, it's pretty obvious where on the form that's going to be, but not all the fields are so obvious - where is Text28, for example?

You can, obviously, put dummy data in each place, fill the form out and observe the results. That's fine, but obviously doesn't help with the Check Boxes. To find out more, you want pdftk dump_data_fields, which includes for this form:

---
FieldType: Text
FieldName: Text28
FieldNameAlt: Brief details of claim
FieldFlags: 12587008
FieldJustification: Left
---
FieldType: Button
FieldName: Check Box34
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Off
FieldStateOption: tickbox No - Does, or will, your claim include any issues under the Human Rights Act 1998?
FieldStateOption: tickbox Yes - Does, or will, your claim include any issues under the Human Rights Act 1998?
---


From this, we can see that Text28 is where we put the "Brief details of claim". It also tells us how to tick the relevant tickbox about the HRA 1998, though it's a bit non-obvious. What you do is include the entire FieldStateOption of the box you want, thus:

<<
/V (tickbox No - Does, or will, your claim include any issues under the Human Rights Act 1998?)
/T (Check Box34)
>> 


So you can edit everything into the FDF file, and keep it in git (and get useful diffs), and then turn it into a filled-out PDF with pdftk form.pdf fill_form data.fdf output form.filled.pdf - the form is neatly filled out, including the tick-boxes!

October

SunMonTueWedThuFriSat
      1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
26
 
27
 
28
 
29
 
30
 
31