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.

  • 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 '16 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 '16 at 12:35

I was able to piece together this solution.

flattenpdf() {
  if [[ $# -ne 2 ]]
    echo "Usage: flattenpdf input.pdf output.pdf"
    return 1
  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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.