Created
January 17, 2016 22:39
-
-
Save Manaphy91/44d74ad8c1800d16537f to your computer and use it in GitHub Desktop.
A simple script useful to rotate a series of scanned pdf files and, after that, concat them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Description | |
| # A simple script useful to rotate a series of scanned pdfs files and, after | |
| # that, concat them. | |
| # manage_scans.bash takes as input three arguments (one of which is optional): | |
| # - source: a path to the folder of source pdf files to manage; | |
| # - destination: a path to the folder where you will see a new pdf file and | |
| # where new temp files will be put | |
| # - output (optional): the name of the file which will be created at the end | |
| # of execution | |
| if [[ -z $1 ]]; then | |
| echo -e "usage $0: $0 source destination [output]\n" | |
| cat $0 | sed -n '3,11 s/# //p' | |
| exit 0 | |
| elif [[ ! -d $1 || ! -r $1 ]]; then | |
| echo "Error: path passed as source isn't a directory or ins't readable!" 1>&2 | |
| exit 1 | |
| elif [[ ! -d $2 || ! -r $2 ]]; then | |
| echo "Error: path passed as destination isn't a directory or ins't readable!" 1>&2 | |
| exit 1 | |
| fi | |
| if [[ -z $3 ]]; then | |
| output="output.pdf" | |
| else | |
| output=$3 | |
| fi | |
| if ! pdftk > /dev/null 2>&1; then | |
| echo -e "Error: pdftk isn't actually installed!\nPlease install it and then rerun this script." 1>&2 | |
| exit 1 | |
| fi | |
| src=${1%/} | |
| dest=${2%/} | |
| i=0 | |
| for arg in `ls -1 $src/*.pdf`; do | |
| file=`basename $arg` | |
| if [ `expr $i % 2` -eq 0 ]; then | |
| pdftk $arg cat 1south output $dest/$file | |
| else | |
| cp -p $arg $dest | |
| fi | |
| i=$(($i + 1)) | |
| done | |
| # concat every temp pdf files to create a new one | |
| pdftk $dest/*.pdf cat output $dest/$output | |
| # delete every temp pdf files in $dest folder | |
| ls $dest/*.pdf | grep -v $output | xargs rm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment