February 2, 2023 3 minutes minutes read Admin

How to Bulk Flatten All PDF Files in a Folder Using pdftk on Linux

This guide is for you if you want to make it easier to handle business documents or just get that stack of digital papers under control. Let's learn more about pdftk, a powerful set of tools for dealing with PDF files.

Step 1: Install pdftk

First things first, we need to install pdftk on your Linux machine. Depending on your flavor of Linux, the commands vary slightly.

For Ubuntu or Debian-based systems:

sudo apt update
sudo apt install pdftk

For Fedora-based systems:

sudo dnf install pdftk

These commands will refresh your repository index and install pdftk.

Step 2: Navigate to Your PDF Folder

Before casting our magic pdftk spell, navigate to the folder containing your PDF files. Open your terminal and use the cd command to change directories:

cd path/to/your/pdf/folder

Replace path/to/your/pdf/folder with the actual path to your documents.

Step 3: Flatten All PDFs

Now, with pdftk installed and your terminal pointed at the right directory, it's time to flatten those PDFs. Run the following command:

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

This loop command goes through each PDF file in the folder, applying the flatten operation which strips all interactive elements like fillable forms, making them static.

Optional: Overwrite Original Files

If you're certain about your changes and prefer to overwrite the original files, use this command instead:

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

This version creates a temporary flattened file, then overwrites the original with this flattened version. Use this with caution!