7

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.

3
  • I have the same issue. Can someone shed light on this? I have form elements on my source pdf. after flatten, my output has no values. version 2.02 on Ubuntu 12.04
    – fbas
    Nov 3, 2016 at 11:54
  • it may be worth noting that my pdf file was modified by MacS Quartz in the Preview tool. This is how I filled in the PDF form fields. when I run this same source PDF through ghostscript, it works, but does note various issues and suggests that Quartz is to blame: **** This file had errors that were repaired or ignored. **** The file was produced by: **** >>>> Mac OS X 10.11.6 Quartz PDFContext <<<< **** Please notify the author of the software that produced this **** file that it does not conform to Adobe's published PDF **** specification.
    – fbas
    Nov 3, 2016 at 12:35
  • pdftk flatten works on all the filled forms I've tried it on, but pdftk can be fussy. Sometimes it won't open a document until I clean it up using qpdf first: try qpdf foo.pdf fixed.pdf then pdftk fixed.pdf bar.pdf flatten
    – Jon Hulka
    Feb 10, 2023 at 22:14

1 Answer 1

8

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.

1
  • 1
    I just spent a weekend to replicate pdftk/itextpdf in another language... PDF forms are hard hard hard hard. You cannot imagine how hard. :( Mar 22, 2022 at 3:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .