How to flatten PDF forms to avoid compatibility errors
Has it ever happened to you that you fill out a PDF form and when you sended to someone else, that person has received an empty form? I will teach you one way to fix these errors by using pdftk
(CLI) or PDF Chain (GUI).
Table of Contents
pdftk
pdftk
(PDF Toolkit) is a great PDF tool (see Encrypting PDFs) and one thing it can do is extracting text from a form and re-add it as a plain text (removing the form inputs).
Script below takes two arguments: the input and output file. It will create a temporary file (inside working directory) with the form info. You can delete the file after the process if you want. After extracting the form inputs, it will fill out the PDF again but it will “flatten” it, i.e. it will convert the form inputs to plain text.
Script (pdf-flatten
):
#!/bin/bash
while [ $# -gt 0 ];do
case $1 in
-i) INPUT=${2// /\\ };;
-o) OUTPUT=${2// /\\ };;
--help) echo "pdf-flatten -i <INPUT> -o <OUTPUT>"
exit 0;;
-h) echo "pdf-flatten -i <INPUT> -o <OUTPUT>"
exit 0;;
esac
shift
done
TEMPFILE=`basename -s .pdf $INPUT`.fdf
sh -c "pdftk $INPUT generate_fdf output $TEMPFILE"
sh -c "pdftk $INPUT fill_form $TEMPFILE output $OUTPUT flatten"
sh -c "rm $TEMPFILE"
Command:
./pdf-flatten -i input.pdf -o output.pdf
PDF Chain
PDF Chain is a graphical frontend for PDFtk, available in Flatpak, and you can flatten a file with just a few clicks. Open PDF Chain and go to “Tools” tab. Select a PDF in the “Document” section and check “Document flatten”. Finally, click “Save As” to save the new flattened file.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: