Skip to content

Instantly share code, notes, and snippets.

@AnonymerNiklasistanonym
Last active March 28, 2019 18:08
Show Gist options
  • Save AnonymerNiklasistanonym/cf54a0231e5c74fb9053e4a2e96ea8f1 to your computer and use it in GitHub Desktop.
Save AnonymerNiklasistanonym/cf54a0231e5c74fb9053e4a2e96ea8f1 to your computer and use it in GitHub Desktop.
Easily scan a double sided document once for every side (just turn the paper stack) through the automatic document feeder and merge with this script these two pdfs to one.
#!/usr/bin/env bash
# Description:
# Easily scan a double sided document once for every side (just turn the paper stack)
# through the automatic document feeder and merge with this script these two pdfs to one.
# Arguments:
# 1) & 2) The paths to the two pdf files that should be combined
# 3) The pdf file name of the combined file
# Example:
# sh .\duplexScanCreator.sh .\DocScan1.pdf .\DocScan2.pdf .\DocScan.pdf
# Get page numbers
PAGE_NUMBER_1=$(pdfinfo $1 | grep Pages | awk '{print $2}')
PAGE_NUMBER_2=$(pdfinfo $2 | grep Pages | awk '{print $2}')
if [ "$PAGE_NUMBER_1" -eq "$PAGE_NUMBER_2" ]
then
echo "The page numbers are equal"
else
echo "The page numbers are not equal"
exit 1
fi
# Create single pages out of the files
pdfseparate $1 a_%d.pdf
pdfseparate $2 b_%d.pdf
# Create an array in the correct order
ARRAY=()
for (( c=1; c<=$PAGE_NUMBER_1; c++ ))
do
ARRAY+=("a_${c}.pdf")
OTHER_NUMBER=$(($PAGE_NUMBER_1 + 1 - $c))
ARRAY+=("b_${OTHER_NUMBER}.pdf")
done
pdfunite "${ARRAY[@]}" $3
echo "File was written: $3"
# Now remove the created files
ARRAY=()
for (( c=1; c<=$PAGE_NUMBER_1; c++ ))
do
rm "a_${c}.pdf"
rm "b_${c}.pdf"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment