[sane-devel] scan2pdf creating PDF from scanned pages

mailinglisten at posteo.de mailinglisten at posteo.de
Mon Dec 7 22:25:43 GMT 2020


Hi there,

need makes creative, some say.

I made a little shell script that creates a single PDF from one ore more
scanned documents. It lets choose from either ADF or flatbed.

It makes 3 assumptions:

the device must understand the --contrast option
it must  understand the --source "Automatic Document Feeder"  option, if
adf is chosen as source
scan area and output PDF both is A4 sized, can be changed in a variable


Tested with a simplex ADF device (Epson BX305F ).

With a simplex ADF device like my Epson, this script processes one sided
documents. It may work fine with a duplex ADF device, not tested.

Maybe this is useful for someone, I attach it as .txt.
If you even consider to include it in your software, do so.
Or use it as a template and improve it or whatever.

best regards

-------------- next part --------------
#!/bin/sh
# ver 1.0  05-dec-2020
#
# Donated to any Unix people who might find it useful
# Licence: choose any you like


clear

SCANDIR="$HOME/tmp/scan"


# scanner resolution default in dpi
scan_res=300



# default page size to scan
# default values for A4 size
scan_x=210
scan_y=297


scancontrast="23"  # gives clearer PDFs, change to your needs, if your device
                   # does not allow to set contrast at all, remove
		   # --contrast option from commands below





if [ ! -d $SCANDIR ]; then
	mkdir -p $SCANDIR
fi


cd $SCANDIR
rm -f $SCANDIR/scan-page-*.jpg
rm -f $SCANDIR/out*.jpg



echo -e "\033[33;44;01mCreate single PDF from scanned document(s)\033[0m"
echo 
echo
echo
echo "This program allows to scan one or more documents and create"
echo "a single PDF file in A4 format from these."; echo
echo "You can choose to scan either with the flatbed unit or an"
echo "automatic document feeder (ADF), if available."; echo
echo "The PDF will be shown after creation using xdg-open."; echo; echo

read -n1 -r -p "(a)utomatic document feeder or (f)latbed scanner or (c)ancel? "
scansource=$REPLY

if [ "$REPLY" == "c" ]; then
	exit 0
fi

REPLAY=""
echo ; echo


echo "File name (without extension, no spaces) for new PDF:"
read -r 
pdfname=$REPLY
REPLY=""

echo; echo
echo "Choose compression for PDF creation"; echo
echo "    30    For simple documents like bills etc. (default)"
echo "    50-80 For higher quality documents" ; echo
read -r -p "Give value or press <ENTER> for default:"
answer=$REPLY

if  [ "$answer" = "" ] ; then

	pdfqual="30"
else
	pdfqual=$answer
fi





case $scansource in 

# When using the flatbed unit
f) 


i=1

while [ $i -gt 0 ]; do
	

	echo; echo
	echo "Put single sheet on flatbed unit, press <ENTER> to start scan"
	read -n1

	scanimage --progress --format jpeg --mode Color --contrast $scancontrast --resolution $scan_res -x $scan_x -y $scan_y -o $SCANDIR/scan-page-$i.jpg 
	

	REPLY=""
	echo; echo
	read -n1 -r -p "Scan one more document (y/n)?"

		if [ "$REPLY" = "n" ] ; then

			echo
			echo "Creating PDF, please wait..."
				# determine scanned pages and convert
				# to pdf
				pages=$(for scans in $(ls scan-page-*.jpg); do echo $scans; done)
             			convert -compress jpeg -quality $pdfqual $pages $pdfname.pdf

				sleep 2
				rm -f $SCANDIR/scan-page-*.jpg


			
			i=0
			echo "This is your new PDF file:"
			ls -l $SCANDIR/$pdfname.pdf
			xdg-open $pdfname.pdf &
			exit 0
		fi

	let i=i+1
        echo; echo
	
done
exit 0

;;


# If the ADF is to be used
a)

   echo ; echo
   echo "Put sheet(s) in your ADF according to your device"
   echo
   read -n1 -r -p "Press <ENTER> to start scanning"
   scanimage --source "Automatic Document Feeder" --batch  --progress --format jpeg --mode Color --contrast $scancontrast --resolution $scan_res -x $scan_x -y $scan_y
   sleep 2
   
   echo "Creating PDF, please wait..."
   pages=$(for scans in $(ls out*.jpg); do echo $scans; done)
   convert -compress jpeg -quality $pdfqual $pages $pdfname.pdf
   sleep 2
   rm -f $pages

   echo
   echo "This is your new PDF:"
   ls -l $SCANDIR/$pdfname.pdf
   xdg-open $pdfname.pdf &

   exit 0

;;

esac


exit


More information about the sane-devel mailing list