Created
May 9, 2023 19:11
-
-
Save erikyo/afd2a6355d2182d221bf61d5658a361d to your computer and use it in GitHub Desktop.
PDF Appender - Append a page to multiple PDF files in a directory using pdftk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if pdftk is installed | |
if ! command -v pdftk &> /dev/null; then | |
echo "pdftk is not installed." | |
echo "Please install pdftk for your operating system." | |
echo "" | |
case "$(uname -s)" in | |
Linux*) | |
echo "To install on Ubuntu/Debian, run: sudo apt-get install pdftk" | |
echo "To install on CentOS/Fedora, run: sudo yum install pdftk" | |
;; | |
Darwin*) | |
echo "To install on macOS, run: brew install pdftk" | |
;; | |
*) | |
echo "Unsupported operating system. Please install pdftk manually." | |
;; | |
esac | |
exit 1 | |
fi | |
# Set the default directory containing the PDF files | |
pdf_dir="pdf-folder" | |
# Set the default filename of the page you want to append to each PDF | |
page_file="cover/cover.pdf" | |
# Set the default output directory | |
output_dir="out" | |
# Print usage message function | |
function usage { | |
echo "Usage: $0 [-d PDF_DIRECTORY] [-p PAGE_FILE] [-o OUTPUT_DIRECTORY] [--help]" 1>&2 | |
echo "" | |
echo "This script relies that you have " | |
echo "" | |
echo "Append a page to each PDF file in a directory." | |
echo "" | |
echo "Options:" | |
echo " -d PDF_DIRECTORY Directory containing the PDF files (default: $pdf_dir)" | |
echo " -p PAGE_FILE Filename of the page to append (default: $page_file)" | |
echo " -o OUTPUT_DIRECTORY Directory to save the modified PDF files (default: $output_dir)" | |
echo " --help Display this help message" | |
echo "" | |
exit 1 | |
} | |
# Parse command line arguments | |
while getopts ":d:p:o:-:" opt; do | |
case ${opt} in | |
d ) pdf_dir="$OPTARG" | |
;; | |
p ) page_file="$OPTARG" | |
;; | |
o ) output_dir="$OPTARG" | |
;; | |
- ) | |
case "${OPTARG}" in | |
help) | |
usage | |
;; | |
*) | |
echo "Invalid option: --$OPTARG" 1>&2 | |
exit 1 | |
;; | |
esac | |
;; | |
\? ) echo "Invalid option: -$OPTARG" 1>&2 | |
usage | |
;; | |
: ) echo "Option -$OPTARG requires an argument." 1>&2 | |
usage | |
;; | |
esac | |
done | |
# Count the number of PDF files in the directory | |
num_files=$(ls -1q "$pdf_dir"/*.pdf | wc -l) | |
# Initialize a counter for the number of processed files | |
count=0 | |
# Loop through each PDF file in the directory | |
for file in "$pdf_dir"/*.pdf; do | |
# Print the name of the PDF file being processed | |
echo "Processing $file..." | |
# Append the page to the PDF using pdftk | |
pdftk "$page_file" "$file" cat output "$output_dir"/"${file##*/}" | |
# Increment the counter | |
count=$((count+1)) | |
# Calculate the progress percentage and print it | |
progress=$((count * 100 / num_files)) | |
echo "----> Progress: $progress%" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment