Pdftk flatten loses fillable field data

pdfpdftk

I'm trying to work out a solution to flatten PDFs with already-filled out fields on a Linux command line. I was messing around with GhostScript at first, but I found that tended to convert all the field data to garbage characters.

When I run this command

pdftk foo.pdf output bar.pdf flatten

and open bar.pdf in a PDF reader, it's flattened, but the data that was in the fields simply isn't present. The PDF is just the same as if it wasn't filled out, minus the fillable fields.

Is there something I'm missing here?

All the solutions I find on Google are about populating empty forms with .fdf files, which doesn't really pertain to my situation.

Best Answer

I was able to piece together this solution.

flattenpdf() {
  if [[ $# -ne 2 ]]
  then
    echo "Usage: flattenpdf input.pdf output.pdf"
    return 1
  fi
  temp=$(mktemp)
  pdftk "$1" generate_fdf output ${temp} 
  pdftk "$1" fill_form ${temp} output "$2" flatten
  rm ${temp}
}

It uses pdftk to generate an fdf file from the filled in pdf and then uses that file in a second run to flatten it. Why pdftk doesn't do something like this in the first place is beyond me.

Related Question