Skip to content

Instantly share code, notes, and snippets.

@Manaphy91
Created January 17, 2016 22:39
Show Gist options
  • Select an option

  • Save Manaphy91/44d74ad8c1800d16537f to your computer and use it in GitHub Desktop.

Select an option

Save Manaphy91/44d74ad8c1800d16537f to your computer and use it in GitHub Desktop.

Revisions

  1. Manaphy91 created this gist Jan 17, 2016.
    53 changes: 53 additions & 0 deletions manage_scans.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/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