Skip to content

Instantly share code, notes, and snippets.

@nshenry03
Created December 8, 2014 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nshenry03/f4a4e8d28f04049c817c to your computer and use it in GitHub Desktop.
Save nshenry03/f4a4e8d28f04049c817c to your computer and use it in GitHub Desktop.
Quick script used to merge some PDFs for St. Paul's Lutheran Church of Calhan and Family Heritage Publishers so that they can print the 100 year history book.
#!/bin/bash -
#===============================================================================
#
# FILE: pdfMerge.sh
#
# USAGE: ./pdfMerge.sh
#
# DESCRIPTION: Merges two PDFs based on page numbers
#
# AUTHOR: Nick Henry (NSH), nicholas.henry@appdirect.com
# CREATED: 12/08/2014 12:12
#===============================================================================
set -o nounset # treat unset variables as errors
#===============================================================================
# PASSED VARIABLES
#===============================================================================
USAGE="
USAGE:
Arguments:
-h|--help: Display usage information
-c|--color-input: Input file name with color pages
-g|--gray-input: Input file name with gray pages
-o|--output: Output file name
-p|--pages: Space separated list of color pages
----------------------------------------------------------
Examples:
${0} -c /tmp/color.pdf -g /tmp/gray.pdf -o /tmp/output.pdf -p '1 4 8 15 32'
${0} --color-input='/tmp/color.pdf' --gray-input='/tmp/gray.pdf' \
--output='/tmp/output.pdf' --pages='1 4 8 15 32'
"
# :WARNING: The quotes around `$@' are essential!
TEMP=`getopt -q -o hc:g:o:p: --long help,color-input:,gray-input:,output:,pages:\
-n "${0}" -- "${@}"`
# :WARNING: The quotes around `$TEMP' are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-h|--help) echo "${USAGE}" ; exit 0 ;;
-c|--color-input) COLOR_IN="${2}" ; shift 2 ;;
-g|--gray-input) GRAY_IN="${2}" ; shift 2 ;;
-o|--output) OUTPUT="${2}" ; shift 2 ;;
-p|--pages) PAGES="${2}" ; shift 2 ;;
--) shift ; break ;;
esac
done
TOTAL_PAGES=$(pdfinfo ${COLOR_IN} | grep "^Pages:\s*" | awk '{print $NF}')
#===============================================================================
# SANITY CHECKS
#===============================================================================
for file in "${COLOR_IN}" "${GRAY_IN}" ; do
if [[ ! -r "${file}" ]] ; then
echo "Can't read: ${file}" ; exit 1
fi
done
if [[ -e "${OUTPUT}" ]] ; then
echo "Output file already exists: ${OUTPUT}" ; exit 1
fi
if [[ -z "${PAGES}" ]] ; then
echo "-p|--pages parameter is required" ; exit 1
fi
#===============================================================================
# MAIN SCRIPT
#===============================================================================
for page in $(seq 1 ${TOTAL_PAGES}) ; do
if [[ $(echo ${PAGES} | grep -c "\b${page}\b") -eq 0 ]] ; then
echo "Page '${page}' should be a gray page"
gs -q -sDEVICE='pdfwrite' -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage="${page}" -dLastPage="${page}" \
-sOutputFile="/tmp/${page}.pdf" ${GRAY_IN} 2>/dev/null
else
echo "Page '${page}' should be a color page"
gs -q -sDEVICE='pdfwrite' -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage="${page}" -dLastPage="${page}" \
-sOutputFile="/tmp/${page}.pdf" ${COLOR_IN} 2>/dev/null
fi
done
eval gs -dBATCH -dNOPAUSE -q -sDEVICE='pdfwrite' -sOutputFile="${OUTPUT}" \
/tmp/{1..${TOTAL_PAGES}}.pdf 2>/dev/null
eval rm -f /tmp/{1..${TOTAL_PAGES}}.pdf
#===============================================================================
# STATISTICS / CLEANUP
#===============================================================================
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment