Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge pdf files
#!/bin/bash
# This script merge all pds found in a folder
# Where $1 is folder name
FOLDER="$1"
FILE_COUNT="$(ls $FOLDER/*.pdf | wc -l)"
NEW_NAME="merged-pdfs-$FILE_COUNT-$(cat /dev/urandom | tr -dc '0-9a-zA-Z' | fold -w 16 | head -n 1).pdf"
CMD="gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$NEW_NAME"
# Get all files and append to command
for file in "$FOLDER"/*.pdf; do CMD="$CMD $file"; done
echo "Folder: $1"
echo "File Count: $FILE_COUNT"
echo "Pdf: $NEW_NAME"
# echo "Cmd: $CMD"
echo ""
echo "Merge in progress..."
ret="$($CMD)"
echo "*** $NEW_NAME is ready."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment