NoPaste

scan2pdf

von Swordsman

SNIPPET_TEXT:
  1. #!/bin/bash
  2. # Skript:       scan2pdf
  3. # Version:      0.3.4
  4. # Purpose:      Scan multiple pages to one final multi-page PDF while trying
  5. #               to keep a good balance between quality and file size.
  6. # Feedback:     puntarenas<at>fastmail.fm
  7.  
  8. # Default options, modify to your needs
  9. TARGETDIR="$HOME/scan2pdf"
  10. TEMPDIR="/tmp"
  11. BUTTON="false"
  12. AUTHOR=""
  13. TITLE=""
  14. KEYWORD=""
  15.  
  16.  
  17. # Internal variables, do not modify
  18. SCRIPTNAME=$(basename $0 )
  19. EXIT_SUCCESS=0
  20. EXIT_FAILURE=1
  21. EXIT_ERROR=2
  22. EXIT_BUG=10
  23. CLEANUP="false"
  24.  
  25. # Echo usage and quit
  26. function usage {
  27.         echo ""
  28.         echo "Note: scan2pdf creates its PDF files in $TARGETDIR,"
  29.         echo "using $TEMPDIR for temporary files."
  30.         echo ""
  31.         echo "Usage: $SCRIPTNAME [-h] [-b] [-c] [-a \"author\"] [-t \"title\"] [-k \"keywords\"] file" >&2
  32.         echo ""
  33.         echo "-h        show this help"
  34.         echo "-b        use the scanners button for batch mode scanning"
  35.         echo "-c        force cleanup of temporary files"
  36.         echo ""
  37.         echo "PDF options (optional):"
  38.         echo ""
  39.         echo "-a        Author to show up in the document properties, quotes needed"
  40.         echo "-t        Title to show up in the document properties, quotes needed"
  41.         echo "-k        Keywords to show up in the document properties, quotes needed"
  42.         echo ""
  43.         echo "file      Name of the resulting PDF without suffix (required)"
  44.         echo ""
  45.  
  46.         [[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
  47. }
  48.  
  49. # Check if target file or temporary files already exist
  50. function initcheck {
  51.         if [[ -f $TARGETDIR/$PDFNAME.pdf ]] ; then echo ""
  52.                 echo "The  file $TARGETDIR/$PDFNAME.pdf already exists."
  53.                 echo "Please choose another filename."
  54.                 exit $EXIT_ERROR
  55.         fi
  56.  
  57.         if [[ -f $TEMPDIR/scan2pdfout*.pgm ]] || [[ -f $TEMPDIR/scan2pdftiff*.tif ]] || [[ -f $TEMPDIR/scan2pdftemp.tif ]] ||  [[ -f $TEMPDIR/scan2pdftemp.pdf ]] ; then echo ""
  58.                 echo "An error occured, temporary files were not deleted."
  59.                 echo "Call $SCRIPTNAME -c to cleanup"
  60.                 exit $EXIT_ERROR
  61.         fi
  62. }
  63.  
  64. # Clean up the temporary directory
  65. function cleanup {
  66.         echo "Cleaning up..."
  67.         rm $TEMPDIR/scan2pdfout*.pgm
  68.         rm $TEMPDIR/scan2pdftiff*.tif
  69.         rm $TEMPDIR/scan2pdftemp.tif
  70.         rm $TEMPDIR/scan2pdftemp.pdf
  71. }
  72.  
  73. # Start batch scanning for scanners with button support
  74. function button {
  75.         echo ""
  76.         echo "Please insert the first page and push your scanner button to begin."
  77.         echo "<Ctrl><c> twice quits batch mode and starts PDF conversion."
  78.         echo ""
  79.         scanimage --mode=Grayscale --batch=$TEMPDIR/scan2pdfout%d.pgm --batch-start=101 --wait-for-button --resolution=300
  80.         # Delete last image as it is invalid
  81.         ls $TEMPDIR/scan2pdfout*.pgm | sort | tail -1 | xargs rm
  82.         usedata
  83. }
  84.  
  85. # Start batch scanning for scanners without button support
  86. function nobutton {
  87.         echo ""
  88.         echo "Please enter the first page and press any key to scan"
  89.         read -s -n 1
  90.         i="101"
  91.         # Scan first page and increase $i
  92.         echo "Scanning page $[$i-100] ..."
  93.         scanimage --mode=Grayscale --resolution=300 > $TEMPDIR/scan2pdfout$i.pgm
  94.         echo ""
  95.         i=$[$i+1]
  96.         # FIXME: Empty the keyboard buffer
  97.         while read -e -s -t 1 junk; do junk=""; done
  98.         # Scan next page or start conversion
  99.         echo "Please enter page $[$i-100] and press any key to scan"
  100.         echo "Press <q> to quit batch mode and start conversion"
  101.         while true ; do
  102.                 read -s -n 1 scanit
  103.                 if [[ $scanit = "q" ]]; then
  104.                         break
  105.                 else    echo "Scanning page $[$i-100] ..."
  106.                         scanimage --mode=Grayscale --resolution=300 > $TEMPDIR/scan2pdfout$i.pgm
  107.                         echo ""
  108.                         i=$[$i+1]
  109.                         echo "Please enter page $[$i-100] and press any key to scan"
  110.                         echo "Press <q> to quit batch mode and start conversion"
  111.                 fi
  112.                 # FIXME: Empty the keyboard buffer
  113.                 while read -e -s -t 5 junk; do junk=""; echo "junk"; done
  114.         done
  115.         echo ""
  116.         usedata
  117. }
  118.  
  119. # Convert raw data and combine it to one single PDF
  120. function usedata {
  121.         # Create TIFFs
  122.         echo "Creating TIFF images..."
  123.         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
  124.         # Create one big TIFF
  125.         echo "Combining TIFF images..."
  126.         tiffcp -c lzw $TEMPDIR/scan2pdftiff* $TEMPDIR/scan2pdftemp.tif
  127.         # Create PDF
  128.         echo "Creating PDF document..."
  129.         tiff2pdf -z $TEMPDIR/scan2pdftemp.tif -o $TEMPDIR/scan2pdftemp.pdf -a "$AUTHOR" -t "$TITLE" -k "$KEYWORD"
  130.         #Optimize for faster viewing
  131.         echo "Optimizing PDF..."
  132.         pdfopt $TEMPDIR/scan2pdftemp.pdf $TARGETDIR/$PDFNAME.pdf
  133.         # Check if anything looks fine
  134.         if [[ -f $TARGETDIR/$PDFNAME.pdf ]] ; then
  135.                 cleanup
  136.                 echo "$PDFNAME.pdf was created at $TARGETDIR"
  137.                 exit $EXIT_SUCCESS
  138.         else    echo ""
  139.                 echo "An error occured, temporary files were not deleted."
  140.                 echo "Call $SCRIPTNAME -c to cleanup"
  141.                 exit $EXIT_ERROR
  142.         fi
  143. }
  144.        
  145. # Check for Options using getopts
  146. while getopts ':hbn:ca:t:k:' OPTION ; do
  147.         case $OPTION in
  148.         h)      usage $EXIT_SUCCESS
  149.                 ;;
  150.         b)      BUTTON="true"
  151.                 ;;
  152.         c)      CLEANUP="true"
  153.                 ;;
  154.         a)      AUTHOR="$OPTARG"
  155.                 ;;
  156.         t)      TITLE="$OPTARG"
  157.                 ;;
  158.         k)      KEYWORD="$OPTARG"
  159.                 ;;
  160.        
  161.         \?)     echo "Unknown option \"-$OPTARG\"." >&2
  162.                 usage $EXIT_ERROR
  163.                 ;;
  164.  
  165.         :)      echo "Option \"-$OPTARG\" needs an argument." >&2
  166.                 usage $EXIT_ERROR
  167.                 ;;
  168.         *)        echo "This should never happen..." >&2
  169.                 usage $EXIT_BUG
  170.                 ;;
  171.         esac
  172. done
  173.  
  174. # Skip already handled arguments
  175. shift $(( OPTIND - 1 ))
  176.  
  177. # Check for cleanup request
  178. if [[ $CLEANUP = true ]]; then cleanup
  179.         exit $EXIT_SUCCESS
  180. fi
  181.  
  182. # Test for at least one remaining argument here and set $PDFNAME
  183. if (( $# < 1 )) ; then
  184.         usage $EXIT_ERROR
  185.         else PDFNAME=$1
  186. fi
  187.  
  188. # Check for the target directory
  189. if [[ ! -d $TARGETDIR ]] ; then mkdir $TARGETDIR
  190.         if [[ ! -d $TARGETDIR ]] ; then exit $EXIT_ERROR
  191.         fi
  192. fi
  193.  
  194. # Check for the temporary directory
  195. if [[ ! -d $TEMPDIR ]] ; then mkdir $TEMPDIR
  196.         if [[ ! -d $TEMPDIR ]] ; then exit $EXIT_ERROR
  197.         fi
  198. fi
  199.  
  200. # Check if target or temporary files already exist
  201. initcheck
  202.  
  203. # Use the scanners button for batch-mode?
  204. if [[ $BUTTON = true ]] ; then button
  205.         else nobutton
  206. fi
  207.  
  208. # Still buggy :(
  209. echo "This should never happen!"       
  210. exit $EXIT_BUG

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN