scan2pdf
von Swordsman- SNIPPET_TEXT:
-
- #!/bin/bash
- # Skript: scan2pdf
- # Version: 0.3.4
- # Purpose: Scan multiple pages to one final multi-page PDF while trying
- # to keep a good balance between quality and file size.
- # Feedback: puntarenas<at>fastmail.fm
- # Default options, modify to your needs
- TARGETDIR="$HOME/scan2pdf"
- TEMPDIR="/tmp"
- BUTTON="false"
- AUTHOR=""
- TITLE=""
- KEYWORD=""
- # Internal variables, do not modify
- SCRIPTNAME=$(basename $0 )
- EXIT_SUCCESS=0
- EXIT_FAILURE=1
- EXIT_ERROR=2
- EXIT_BUG=10
- CLEANUP="false"
- # Echo usage and quit
- function usage {
- echo ""
- echo "Note: scan2pdf creates its PDF files in $TARGETDIR,"
- echo "using $TEMPDIR for temporary files."
- echo ""
- echo "Usage: $SCRIPTNAME [-h] [-b] [-c] [-a \"author\"] [-t \"title\"] [-k \"keywords\"] file" >&2
- echo ""
- echo "-h show this help"
- echo "-b use the scanners button for batch mode scanning"
- echo "-c force cleanup of temporary files"
- echo ""
- echo "PDF options (optional):"
- echo ""
- echo "-a Author to show up in the document properties, quotes needed"
- echo "-t Title to show up in the document properties, quotes needed"
- echo "-k Keywords to show up in the document properties, quotes needed"
- echo ""
- echo "file Name of the resulting PDF without suffix (required)"
- echo ""
- [[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
- }
- # Check if target file or temporary files already exist
- function initcheck {
- if [[ -f $TARGETDIR/$PDFNAME.pdf ]] ; then echo ""
- echo "The file $TARGETDIR/$PDFNAME.pdf already exists."
- echo "Please choose another filename."
- exit $EXIT_ERROR
- fi
- if [[ -f $TEMPDIR/scan2pdfout*.pgm ]] || [[ -f $TEMPDIR/scan2pdftiff*.tif ]] || [[ -f $TEMPDIR/scan2pdftemp.tif ]] || [[ -f $TEMPDIR/scan2pdftemp.pdf ]] ; then echo ""
- echo "An error occured, temporary files were not deleted."
- echo "Call $SCRIPTNAME -c to cleanup"
- exit $EXIT_ERROR
- fi
- }
- # Clean up the temporary directory
- function cleanup {
- echo "Cleaning up..."
- rm $TEMPDIR/scan2pdfout*.pgm
- rm $TEMPDIR/scan2pdftiff*.tif
- rm $TEMPDIR/scan2pdftemp.tif
- rm $TEMPDIR/scan2pdftemp.pdf
- }
- # Start batch scanning for scanners with button support
- function button {
- echo ""
- echo "Please insert the first page and push your scanner button to begin."
- echo "<Ctrl><c> twice quits batch mode and starts PDF conversion."
- echo ""
- scanimage --mode=Grayscale --batch=$TEMPDIR/scan2pdfout%d.pgm --batch-start=101 --wait-for-button --resolution=300
- # Delete last image as it is invalid
- ls $TEMPDIR/scan2pdfout*.pgm | sort | tail -1 | xargs rm
- usedata
- }
- # Start batch scanning for scanners without button support
- function nobutton {
- echo ""
- echo "Please enter the first page and press any key to scan"
- read -s -n 1
- i="101"
- # Scan first page and increase $i
- echo "Scanning page $[$i-100] ..."
- scanimage --mode=Grayscale --resolution=300 > $TEMPDIR/scan2pdfout$i.pgm
- echo ""
- i=$[$i+1]
- # FIXME: Empty the keyboard buffer
- while read -e -s -t 1 junk; do junk=""; done
- # Scan next page or start conversion
- echo "Please enter page $[$i-100] and press any key to scan"
- echo "Press <q> to quit batch mode and start conversion"
- while true ; do
- read -s -n 1 scanit
- if [[ $scanit = "q" ]]; then
- break
- else echo "Scanning page $[$i-100] ..."
- scanimage --mode=Grayscale --resolution=300 > $TEMPDIR/scan2pdfout$i.pgm
- echo ""
- i=$[$i+1]
- echo "Please enter page $[$i-100] and press any key to scan"
- echo "Press <q> to quit batch mode and start conversion"
- fi
- # FIXME: Empty the keyboard buffer
- while read -e -s -t 5 junk; do junk=""; echo "junk"; done
- done
- echo ""
- usedata
- }
- # Convert raw data and combine it to one single PDF
- function usedata {
- # Create TIFFs
- echo "Creating TIFF images..."
- ls $TEMPDIR/scan2pdfout*.pgm | while read p; do echo $p; q=`echo $( basename $p )| sed 's/scan2pdfout\(.*\)\.pgm/scan2pdftiff\1.tif/'`; echo $q; cat $p | pgmtopbm -t -v 0.6 | pnmtotiff -g4 > $TEMPDIR/$q ; done
- # Create one big TIFF
- echo "Combining TIFF images..."
- tiffcp -c lzw $TEMPDIR/scan2pdftiff* $TEMPDIR/scan2pdftemp.tif
- # Create PDF
- echo "Creating PDF document..."
- tiff2pdf -z $TEMPDIR/scan2pdftemp.tif -o $TEMPDIR/scan2pdftemp.pdf -a "$AUTHOR" -t "$TITLE" -k "$KEYWORD"
- #Optimize for faster viewing
- echo "Optimizing PDF..."
- pdfopt $TEMPDIR/scan2pdftemp.pdf $TARGETDIR/$PDFNAME.pdf
- # Check if anything looks fine
- if [[ -f $TARGETDIR/$PDFNAME.pdf ]] ; then
- cleanup
- echo "$PDFNAME.pdf was created at $TARGETDIR"
- exit $EXIT_SUCCESS
- else echo ""
- echo "An error occured, temporary files were not deleted."
- echo "Call $SCRIPTNAME -c to cleanup"
- exit $EXIT_ERROR
- fi
- }
- # Check for Options using getopts
- while getopts ':hbn:ca:t:k:' OPTION ; do
- case $OPTION in
- h) usage $EXIT_SUCCESS
- ;;
- b) BUTTON="true"
- ;;
- c) CLEANUP="true"
- ;;
- a) AUTHOR="$OPTARG"
- ;;
- t) TITLE="$OPTARG"
- ;;
- k) KEYWORD="$OPTARG"
- ;;
- \?) echo "Unknown option \"-$OPTARG\"." >&2
- usage $EXIT_ERROR
- ;;
- :) echo "Option \"-$OPTARG\" needs an argument." >&2
- usage $EXIT_ERROR
- ;;
- *) echo "This should never happen..." >&2
- usage $EXIT_BUG
- ;;
- esac
- done
- # Skip already handled arguments
- shift $(( OPTIND - 1 ))
- # Check for cleanup request
- if [[ $CLEANUP = true ]]; then cleanup
- exit $EXIT_SUCCESS
- fi
- # Test for at least one remaining argument here and set $PDFNAME
- if (( $# < 1 )) ; then
- usage $EXIT_ERROR
- else PDFNAME=$1
- fi
- # Check for the target directory
- if [[ ! -d $TARGETDIR ]] ; then mkdir $TARGETDIR
- if [[ ! -d $TARGETDIR ]] ; then exit $EXIT_ERROR
- fi
- fi
- # Check for the temporary directory
- if [[ ! -d $TEMPDIR ]] ; then mkdir $TEMPDIR
- if [[ ! -d $TEMPDIR ]] ; then exit $EXIT_ERROR
- fi
- fi
- # Check if target or temporary files already exist
- initcheck
- # Use the scanners button for batch-mode?
- if [[ $BUTTON = true ]] ; then button
- else nobutton
- fi
- # Still buggy :(
- echo "This should never happen!"
- exit $EXIT_BUG
Quellcode
Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN