Skip to content

Instantly share code, notes, and snippets.

@bjbishop
Last active April 24, 2020 22:32
Show Gist options
  • Save bjbishop/bacca9f96b1d8054be703848de2ee3c6 to your computer and use it in GitHub Desktop.
Save bjbishop/bacca9f96b1d8054be703848de2ee3c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e -o nounset
PDFTK_PATH="$HOME/src"
PDFTK_URL="https://gitlab.com/pdftk-java/pdftk/-/jobs/517065828/artifacts/raw/build/libs/pdftk-all.jar?inline=false"
if ! [ -d "$PDFTK_PATH" ]
then
printf "Creating the src path to install the pdftk-all tool"
mkdir -p "$PDFTK_PATH"
fi
if ! command -v curl > /dev/null 2>&1
then
printf "Not able to find the curl command on this system."
printf "Please download this file manually and place it into the %s" "$PDFTK_PATH"
printf "%s" "$PDFTK_URL"
exit 1
fi
if ! [ -f "$PDFTK_PATH/pdftk-all.jar" ]
then
printf "The pdftk-all tool was not found, downloading a new copy"
curl "$PDFTK_URL" -s -o "$PDFTK_PATH/pdftk-all.jar"
fi
if ! command -v java > /dev/null 2>&1
then
printf "The java command wasn't found on your system, trying to install it."
curl "https://github.com/AdoptOpenJDK/openjdk10-binaries/releases/download/jdk-10.0.2%2B13.1/OpenJDK10U-jre_x64_mac_hotspot_10.0.2_13.tar.gz" -L -s -o "$PDFTK_PATH/OpenJDK10U-jre_x64_mac_hotspot_10.0.2_13.tar.gz"
cd "$PDFTK_PATH" || exit 1
tar xzf OpenJDK10U-jre_x64_mac_hotspot_10.0.2_13.tar.gz
rm OpenJDK10U-jre_x64_mac_hotspot_10.0.2_13.tar.gz
export PATH=$PATH:"$PDFTK_PATH/jdk-10.0.2+13-jre/Contents/Home/bin"
export JAVA_HOME="$PDFTK_PATH/jdk-10.0.2+13-jre/Contents/Home"
java -version
cd - || exit 1
else
if [ -f combined.pdf ]
then
printf "\n\nThis tool will create a file called combined.pdf and a file with\n"
printf "that name was already found here: %s/combined.pdf\n\n" "$PWD"
printf "Please rename or remove this combined.pdf file."
exit 1
fi
if [ $# -eq 0 ]
then
printf "\n\nType in the names of the PDF files to be combined after the name of the program seperated by a spaces.\n\n"
printf "Make sure to use 'quotes' around any filename with a space already in it.\n\n\n"
printf "Example: %s my_file.pdf 'My Other Document.PDF' somefile.pdf" "$0"
exit 1
fi
java -jar "$PDFTK_PATH/pdftk-all.jar" "$@" cat output combined.pdf
printf "Done! Created: %s/combined.pdf\n\n" "$PWD"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment