Fix for XSane making big pdfs

Are you using Linux as your primary desktop/office system? Yes.
Do you use a scanner? Yes.
Are you using XSane to scan documents and save them as .pdf files? Yes.

So you very likely encountered the fact, that the .pdfs that are created are HUGE (~5MB per A4 page or so).

Here is a quick fix using some dirty shell scripts that automates compressing every newly created scan that was created with XSane.

Step 1:

You need a “bin” folder in your home directory.

mkdir /home/$USER/bin

make sure that this folder is in your $PATH variable, e.g. that the Linux shell will look in this folder for finding programs you call on the command line. This is done (if not already the case) by adding the following to your ~/.profile

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Step 2:

Add the following scripts to your /home/$USER/bin

/home/$USER/bin/pdfcompress

#!/bin/bash

if [[ -z $1 || "$1" == "--help" ]]; then
cat <<EOF
pdfcompress
  a simple wrapper around gs to compress PDFs (e.g. produced by xsane)

usage:
  $ pdfcompress <inputfile.pdf> [<quality>]
  
  <quality> (optional)  can be screen (low)
                               ebook (medium,default)
                               prepress (high)
2014 by MW
EOF

exit
fi


if [ -z $2 ]; then
quality="ebook"
else
quality=$2
fi

out=$(mktemp)
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/$quality -sOutputFile=$out $1 && cp $out $1
rm $out


/home/$USER/bin/scan_compress_loop.sh

#!/bin/bash

cd $HOME/scan
while true; do
for i in scan*.pdf; do
  if [ ! -e ".${i}_processed" ]; then
    sleep 2
    pdfcompress $i
    > .${i}_processed
  fi
done
sleep 2
done


/home/$USER/bin/xsane

#!/bin/bash
scan_compress_loop.sh&
loop_pid=$!
/usr/bin/xsane $@
kill -9 $loop_pid


Step 3:

When you now start “xsane” from the terminal or Alt+F2, you will have this “scan_compress_loop.sh” task in the background that will watch newly created pdfs and compress them once and then leave them be. The background task terminates if the xsane window is closed.

IMPORTANT: For this to work, your scan PDFs have to be created in $HOME/scan and the filename has to begin with “scan”, e.g. “$HOME/scan/scan_005.pdf”.

Enjoy Xsane and your ~300 kB PDFs 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.