Search

Debian - Setup Scan-to-folder server for HP AIO scanner

Contents[Hide]

Debian

HP Multi-Function Network Printers support is very good under Linux thanks to HP Linux Imaging and Printing (HPLIP).

But one important functionality is still missing : Scan-to-folder. You can not make your scanned documents easily available from a public network share.

This article explains how to setup a Debian server daemon to become a Scan-to-folder server for HP Multi-Function Network Printer/Scanner.

If this destination folder is shared ... you will have a direct access to your scanned documents from any PC.

debian-scan-to-folder-server

This Scan-to-folder service has been tested on a Debian Squeeze server and with a HP CM1312nfi All-In-One printer/scanner.

It should be compatible with many Linux flavours and with many modern HP multi-function network printers.

1. Main Principle

This Scan-to-Folder server is using some undocumented web page provided by the local web server embedded in many HP printer/scanner.

The page that this guide is using is http://your.printer.ip.address/hp/device/notifications.xml.

It is providing some information about the status of the scanner Scan button and of the Auto Document Feed tray.

Some explainations are available in this article.

So, the main principle of this Scan-to-folder server is quite simple :

  • A deamon is running on your Debian server
  • It periodically checks the Scan button on your AIO HP Scanner
  • When button is pressed, documents are scanned
  • Documents are scanned either form Auto Feed tray if available or from the Glass Window
  • Documents are converted automatically to the target format
  • They are made available in a specific folder on the server

If this specific folder is shared thru Samba, all scanned documents may be directly available to any computer on the Local Network.

2. Install Dependencies

First thing to do is to install all dependencies needed by the Scan-to-Folder service.

  • imagemagick, will be used to convert the scanned file to the target graphical format (jpg, png, pdf, ...).
  • cups, which is needed by HPLIP
  • avahi-daemon, is also needed by HPLIP

# apt-get install imagemagick cups cups-bsd avahi-daemon

3. Install HPLIP

To be compatible with latest HP AIO devices, we won't install hplip from repository, but from HP Linux Imaging and Printing site.

This will allow to be compatible with the latest devices.

As a pre-requisite, we will remove any previous hplip installation.

As we are on a server which may not have any X environment, we will run the installation with a nox11 option.

We will also select the custom option, not to install any GUI and fax modules.


# apt-get purge hplip
# apt-get autoremove
# wget http://prdownloads.sourceforge.net/hplip/hplip-3.13.2.run
# sh ./hplip-3.13.2.run --nox11
error: You are running the installer as root. It is highly recommended that you run the installer as
error: a regular (non-root) user. Do you still wish to continue?
Continue with installation (y=yes, n=no*, q=quit) ? y

Creating directory hplip-3.13.2
Verifying archive integrity... All good.
Uncompressing HPLIP 3.13.2 Self Extracting Archive ...
...
HP Linux Imaging and Printing System (ver. 3.13.2)
HPLIP Installer ver. 5.1
...

INSTALLATION MODE
-----------------
Automatic mode will install the full HPLIP solution with the most common options.
Custom mode allows you to choose installation options to fit specific requirements.

Please choose the installation mode (a=automatic*, c=custom, q=quit) : c
...

DISTRO/OS CONFIRMATION
----------------------
Distro appears to be Debian 6.0.6.
Is "Debian 6.0.6" your correct distro/OS and version (y=yes*, n=no, q=quit) ? y

SELECT HPLIP OPTIONS
--------------------
You can select which HPLIP options to enable. Some options require extra dependencies.

Do you wish to enable 'Network/JetDirect I/O' (y=yes*, n=no, q=quit) ? y
Do you wish to enable 'Graphical User Interfaces (Qt4)' (y=yes*, n=no, q=quit) ? n
Do you wish to enable 'PC Send Fax support' (y=yes*, n=no, q=quit) ? n
Do you wish to enable 'Scanning support' (y=yes*, n=no, q=quit) ? y
Do you wish to enable 'HPLIP documentation (HTML)' (y=yes*, n=no, q=quit) ? y

PRINTER SETUP
-------------
Please make sure your printer is connected and powered on at this time.
error: hp-setup failed. Please run hp-setup manually.
...

At the end, you may get a hp-setup error ... everything is ok.

As we are in a server console only environment, hp-setup should be launched with -i option (interactive).

4. Run hp-setup

Once installation is over, we must run hp-setup in interactive mode. This mode allows hp-setup to be run from console in a full text mode.

In the connection (I/O) type select 1 for a network printer.

If your PPD is not detected, select it manually from the ppd/hpcups/xxxxx.ppd.gz file in the hplip-x.xx.x directory created in the installation directory.

Select options according to your setup and select to download plugin from HP.

This may take some long minutes.


# hp-setup -i
...

Num Connection Description
Type
-------- ---------- ----------------------------------------------------------
0* usb Universal Serial Bus (USB)
1 net Network/Ethernet/Wireless (direct connection or JetDirect)

Enter number 0...1 for connection type (q=quit, enter=usb*) ? 1
...

error: No PPD found for model color_laserjet_cm1312nfi_mfp using old algorithm.
Would you like to specify the path to the correct PPD file to use (y=yes, n=no*, q=quit) ? y
Please enter the full filesystem path to the PPD file to use (q=quit) :./hplip-3.13.2/ppd/hpcups/hp-color_laserjet_cm1312nfi_mfp-pcl3.ppd.gz
...

5. Test Scanner

Once installation is over, first thing is to check that you can find your HP All-in-one scanner :

# scanimage -L
device 'hpaio:/net/HP_Color_LaserJet_CM1312nfi_MFP?zc=NPI49F2D1' is a Hewlett-Packard HP_Color_LaserJet_CM1312nfi_MFP all-in-one

As your scanner is found, you can try to do a first scan :

# scanimage --format=pnm --mode=Color --resolution=150 > ./test.pnm

If everything is ok, you should get a test.pnm file in your current directory.

You should not go ahead as long as you don't succeed to this simple test.

6. Setup Scan-to-folder

6.1. Daemon configuration

To follow Debian standard, default configuration of Scan-to-folder will be done in /etc/default/hp-scan2folder.

This is the file where you can adjust the sevice to your taste & configuration :

  • SCAN_IP : IP address of your network scanner
  • SCAN_FOLDER : Server local path where to save scanned files
  • SCAN_EXTENSION : standard extension of scanned files (jpg, png, pdf, ...)
  • SCAN_DPI : Resolution in DPI of your scans. possible values depend on your scanner
  • SCAN_DELAY : Time in seconds between 2 scanner button pressed check
  • SCAN_LOG : Server path of log file. This file will log all scanning operations.

/etc/default/hp-scan2folder

# ----------------------------------------------------
# Default options for HP Scan2folder feature
#
# Sourced by /etc/init.d/hp-scan2folder
# ----------------------------------------------------

# Multifunction printer IP address
SCAN_IP="192.168.x.x"

# Folder where to store scanned documents
SCAN_FOLDER="/your/server/scan/folder/target"

# type of document to generate (jpg, png, pdf, ...)
SCAN_EXTENSION="jpg"

# resolution of scan
SCAN_DPI="300"

# button pressed scan delay (in seconds)
SCAN_DELAY="2"

# log file
SCAN_LOG="/var/log/hp-scan2folder.log"

6.2. Scan-to-folder Daemon

The Scan-to-folder daemon is a shell script placed under /usr/sbin/hp-scan2folder

This daemon is an infinite loop that checks every few seconds if :

  • scan button is pressed
  • documents are loaded in the Auto Document Feed tray

According to these status, the daemon will start to scan documents from Glass Window of from Auto Document Feed tray.

Scanned document will be converted according to the target file extension.

They will be available in the target folder described in the default configuration.

/usr/sbin/hp-scan2folder

#!/bin/sh
# ----------------------------------------------
#  Scan to Folder utility
#  for network HP multifunction printer/scanner
#  
#  Depends on :
#   * imagemagick (for mogrify)
#   * avahi-daemon (for hplip)
#   * hplip
#
#  Revision history :
#  24/02/2013 - Creation by N. Bernaerts
# ---------------------------------------------

# set PATH
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# source default parameters file
#   /etc/default/hp-scan2folder
[ -f "/etc/default/hp-scan2folder" ] && . /etc/default/hp-scan2folder

# if log file doesn't exist, create it
[ ! -f "$SCAN_LOG" ] &&  /bin/touch "$SCAN_LOG"

# go to scan folder
cd $SCAN_FOLDER

#  Scan-to-Folder loop
while [ true ]
do    
  # check if scan button is pushed
  SCAN_START=`wget --quiet -O - http://$SCAN_IP/hp/device/notifications.xml | grep "<StartScan>" | sed "s/^.*<StartScan>\([01]\)<\/StartScan>.*$/\1/g"`

  # if scan button has been pressed or scan is forced
  if [ "$SCAN_START" = "1" ]
  then
    # getting current date
    SCAN_DATE=`date '+%Y%m%d-%Hh%Mm%Ss'`

    # check if documents are placed in Auto Feed
    SCAN_ADF=`wget --quiet -O - http://$SCAN_IP/hp/device/notifications.xml | grep "<ADFLoaded>" | tail --lines=1 | sed "s/^.*<ADFLoaded>\([01]\)<\/ADFLoaded>.*$/\1/g"`

    # Document scan from the glass window
    if [ "$SCAN_ADF" = "0" ]
    then
      # generate timestamped filename
      SCAN_FILE=$SCAN_FOLDER/$SCAN_DATE-scan.pnm

      # scan image with needed resolution
      [ -f "$SCAN_LOG" ] && echo "`date '+%Y%m%d - %H:%M.%S'` : Scanning from Glass Window" >> "$SCAN_LOG"
      scanimage --format=pnm --mode=Color --resolution=$SCAN_DPI > $SCAN_FILE

      # convert to the proper format
      if [ -f "$SCAN_FILE" ]
      then
        [ -f "$SCAN_LOG" ] && echo "`date '+%Y%m%d - %H:%M.%S'` : Converting $SCAN_FILE to $SCAN_EXTENSION format" >> "$SCAN_LOG"
        mogrify -format $SCAN_EXTENSION $SCAN_FILE
        rm $SCAN_FILE
      fi      

    # Document(s) scan from Auto Document Feed
    else
      # generate timestamped filename base
      SCAN_MASK=`date '+%Y%m%d-%Hh%Mm%Ss-scan'`

      # scan all documents in the Automatic Document Feed
      [ -f "$SCAN_LOG" ] && echo "`date '+%Y%m%d - %H:%M.%S'` : Scanning from Automatic Document Feed" >> "$SCAN_LOG"
      scanimage --format=pnm --mode=Color --resolution=$SCAN_DPI --batch="$SCAN_FOLDER/$SCAN_MASK%d.pnm" --batch-start=1 --source ADF 2>&1

      # convert documents to the proper format
      for SCAN_FILE in "$SCAN_FOLDER/$SCAN_MASK*.pnm"
      do
        [ -f "$SCAN_LOG" ] && echo "`date '+%Y%m%d - %H:%M.%S'` : Converting $SCAN_FILE to $SCAN_EXTENSION format" >> "$SCAN_LOG"
        mogrify -format $SCAN_EXTENSION $SCAN_FILE
        rm $SCAN_FILE
      done
    fi

  # else, wait for $SCAN_DELAY seconds before checking back the scanner button state
  else
    # wait for 1 second
    sleep $SCAN_DELAY
  fi
done

This script daemon should be executable :

# chmod +x /usr/sbin/hp-scan2folder

6.3. Scan-to-folder Service

The Scan-to-folder daemon should be started by a proper debian service.

This service should be declared in /etc/init.d.

Its main job will be to start and stop the scan-to-folder daemon properly.

/etc/init.d/hp-scan2folder

#! /bin/sh
### BEGIN INIT INFO
# Provides:          hp-scan2folder
# Required-Start:    $cups $saned $remote_fs $syslog
# Required-Stop:     $cups $saned $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Scan-to-folder server
# Description:       Daemon script to run the scan-to-folder script for HP All-in-one printers
#                    Placed in /etc/init.d.
### END INIT INFO

# Author: Nicolas Bernaerts <This email address is being protected from spambots. You need JavaScript enabled to view it.>;

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Scan-to-folder server for HP All-in-one printers"
NAME="hp-scan2folder"
DAEMON=/usr/sbin/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Define LSB log_* functions,
# depend on lsb-base (>= 3.2-14) to ensure that this file is present and status_of_proc is working.
. /lib/lsb/init-functions

# Function that starts the daemon/service
do_start()
{
  # If needed, start the daemon : 0 daemon started, 1 daemon already running, 2 daemon could not be started
  if [ -f "$PIDFILE" ]
  then
    echo "$NAME already running"
    RETVAL="1"
  else
    start-stop-daemon --start --background --quiet --make-pidfile --pidfile $PIDFILE --exec $DAEMON
    RETVAL="$?"
    [ "$RETVAL" = "0" ] &&  echo "$NAME started"
  fi
 
  return "$RETVAL"
}

# Function that stops the daemon/service
do_stop()
{
  # Stop the daemon : 0 daemon stopped, 1 daemon already stopped, 2 daemon could not be stopped
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  RETVAL="$?"
  [ "$RETVAL" = "0" ] && echo "$NAME stopped"
  [ "$RETVAL" = "1" ] && echo "$NAME was not running"

  # remove pid file
  rm -f $PIDFILE

  return "$RETVAL"
}

# deal with different parameters : start, stop & status
case "$1" in
  #  start service
  start)
    do_start
    ;;
  # stop service
  stop)
    do_stop
    ;;
  # restart service
  restart)
    do_stop
    do_start
    ;;
  # get service status
  status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
  # unknown command, display help message
  *)
    echo "Usage : $SCRIPTNAME {start|stop|restart|status}" >&2
    exit 3
    ;;
esac

This script daemon should be executable :

# chmod +x /etc/init.d/hp-scan2folder

Last step is to declare the service for automatic startup and shutdown. You also need to start it for the first time.

# update-rc.d hp-scan2folder defaults
# service hp-scan2folder start

7. Do your first Scan-to-folder

Everything is now ready to do your first scan.

As soon as you press the Scan button on your scanner, it should indicate that it waits for a computer and automatically start to scan after a short time.

The scanning will be available under /your/server/scan/folder/target

If you share this directory, your scans may be available to any computer on your LAN ...

A complete log of all scanning operations is available under /var/log/hp-scan2folder.log

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus