Skip to content

Instantly share code, notes, and snippets.

@jacksonporter
Last active May 17, 2020 23:32
Show Gist options
  • Save jacksonporter/18a8acaf0bae8c2c901ea2b9d251691e to your computer and use it in GitHub Desktop.
Save jacksonporter/18a8acaf0bae8c2c901ea2b9d251691e to your computer and use it in GitHub Desktop.
Wrapper over the gs operation to stitch PDFs. Save as a script on your local, or slightly modify and you can throw it in a function.
#!/usr/bin/env bash
set -e
if [[ -z "$(which gs)" ]]; then
printf 'Ghostscript (gs) is not installed, please install it to continue.\n\tExample: `brew install gs`\n'
exit 1
fi
usage="Usage:\n\tall_pdf_files_to_combined_pdf RESULTING_PDF_FILE_NAME pdf1 pdf2 ...\n"
output_file_name="${1}"
if [[ -z "${@}" ]]; then
printf "${usage}\n"
exit 1
fi
shift
if [[ -z "${output_file_name}" ]] || [[ -z "${@}" ]]; then
printf "${usage}\n"
exit 1
fi
printf "Going to combine all files in this directory into a single PDF.\n\n"
printf "Output file name: ${output_file_name}\n"
printf "Files to append together:\n"
args_str=""
for arg in "${@}"; do
args_str="${args_str}\"${arg}\" "
printf "\t${arg}\n"
done
printf "\n"
command_str="gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\"${output_file_name}\" ${args_str}"
printf "\nResulting command to be run:\n\t${command_str}\n"
printf "${command_str}" | sh
printf "\nDone."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment