Skip to content

Instantly share code, notes, and snippets.

@groundcat
Last active March 17, 2023 02:15
Show Gist options
  • Save groundcat/4b62bbd6e139dd6cde952ea5ad86d94a to your computer and use it in GitHub Desktop.
Save groundcat/4b62bbd6e139dd6cde952ea5ad86d94a to your computer and use it in GitHub Desktop.
Flatten all PDF files in a folder on Linux

To flatten all PDF files in a folder, you can use the command-line tool called pdftk. It is a powerful toolkit for working with PDF files, and it is available on most Linux distributions.

First, install pdftk on your system if you haven't already. For Ubuntu or Debian-based systems, you can use:

sudo apt update
sudo apt install pdftk

For Fedora-based systems, you can use:

sudo dnf install pdftk

Once pdftk is installed, you can use it to flatten all PDF files in a folder with a simple shell script. Open your terminal, navigate to the folder containing the PDF files, and run the following command:

for file in *.pdf; do pdftk "$file" output "flattened_$file" flatten; done

This command will go through all PDF files in the current folder and create a new flattened version of each file with the prefix "flattened_". All fillable forms and interactive elements will be removed from the flattened PDF files.

If you want to overwrite the original files instead of creating new ones, you can use the following command:

for file in *.pdf; do pdftk "$file" output "${file%.pdf}_temp.pdf" flatten && mv "${file%.pdf}_temp.pdf" "$file"; done

This command will create a temporary flattened file for each PDF and then overwrite the original file with the flattened version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment