Search

Ubuntu - HP Laserjet Multifunction Scan-to-Folder

Contents[Hide]

Ubuntu

hp-cm1312-mfpHP 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.

In fact, under Linux, you can scan documents easily, but you need to do it thru a graphical interface like XSane or Simple Scan.

Natively, there is no possibility to control the scanning in a scan-to-folder way.

This article will analyse the HP interfaces available and will explain how to setup a shell script to do :

  • a one-click scan interface, that will immediately scan current document straight in a default folder
  • a scan server, that will wait for you to press the Start Scan button from the scanner to scan current document in a default folder

This script will allow you to setup default document folder & format (jpg, png, pdf, ...). It will handle documents from the scanner Glass Window and from Auto Document Feed tray.

It has been tested on an HP MFP 1312, but it should be compatible with many modern HP multi-function network printer.

Before going ahead, make sure your network scanner is properly installed and configured.

A guide is available under Ubuntu - Install HP Laserjet Multifunction (print & scan).

1. Install Tools

First thing to do is to install the tools needed by the Scan-to-Folder script.

imagemagick will be used to convert the scanned file to the target graphical format (jpg, png, pdf, ...).

# sudo apt-get install imagemagick

YAD is a GUI very similar to Zenity, but with lot more possibilities.

It can be launched from command-line, display multiple fields and display progression bar.

We will use it for the user interface part of the Scan-to-Folder script.

As it is not available in Ubuntu repositories, we will get it from well-known WebUpd8 PPA.

# sudo add-apt-repository ppa:webupd8team/y-ppa-manager
# sudo apt-get update
# sudo apt-get install yad

2. HP MultiFunction Printers undocumented Web pages

HP MultiFunction Printers are running a web server that can be accessed with a web browser.

Official HP documentation is describing this Web interface in details.

But some undocumented Web pages are also available, providing some very useful informations !

One very interesting page available is http://your.printer.ip.address/hp/device/notifications.xml

http://your.printer.ip.address/hp/device/notifications.xml

<Notifications xsi:schemaLocation="http://www.hp.com/schemas/imaging/pls/dev/1.0 Notifications.xsd">
  <ScanToNotifications>
    <ScanToDeviceDisplay/>
    <ScanToHostID/>
    <ScanToType/>
    <ScanToNotSetup>1</ScanToNotSetup>
    <ADFLoaded>0</ADFLoaded>
  </ScanToNotifications>
  <StartScanNotifications>
    <StartScan>0</StartScan>
    <ADFLoaded>0</ADFLoaded>
  </StartScanNotifications>
  <FaxNotifications>
    <FaxReceiveFunction>1</FaxReceiveFunction>
    <FaxPrinting>0</FaxPrinting>
    <LastFaxLogEntry>
    <EntryID>3</EntryID>
    <Type>7</Type>
    <FaxNumber/>
    <TimeDate>20120709115950</TimeDate>
    <NumPages>0</NumPages>
    <ResultCode>3</ResultCode>
    </LastFaxLogEntry><FaxMasterHostID/>
    <FaxUploadState>1</FaxUploadState>
    <FaxLogChangeIndicator>7</FaxLogChangeIndicator>
    <FaxForwardEnabled>0</FaxForwardEnabled>
    <FaxForwardNumber/>
  </FaxNotifications>
</Notifications>

You can notice 2 very interesting XML keys :

  • <ADFLoaded> : Set to 1 if some pages are in the Auto Document Feed tray. Set to 0 otherwise.
  • <StartScan> : Set to 1 if the Start Scan button has been pressed. Set to 0 otherwise.

So, by just reading this page, we are able to detect :

  • if the Start Scan button has been pressed,
  • if the scan has to be done from the Glass Window (default) or from documents loaded in the Auto Document Feed tray.

Make sure your printer is providing this XML page and is behaving as expected before going further.

3. Scan-to-Folder Script

3.1. Features

Exploiting these two very important informations, we are now able to create a Scan-to-Folder script.

The principle used within scan2folder.sh script is very simple :

  1. You place your document in the scanner (Auto Document Feed tray or Glass Window)
  2. You launch the script
  3. Depending on parameter, you are asked or not to select the resolution of the scanned picture (in dpi)

    ubuntu-hp-scantofolder-dpi
  4. Depending on the parameter, the document scan starts immediately or waits for you to press Start Scan button on the scanner
  5. If one or more documents are found in the Auto Document Feed tray, they are scanned from there
  6. Otherwise, document is scanned from the Glass Window

    ubuntu-hp-scantofolder-scan
  7. Scanned documents are directly available under a specified directory
  8. They are converted automatically to whatever format you've configured (png, jpg, pdf, ...)

With that Scan-to-Folder feature, you don't need to start any graphical application to do a single scan or to scan a serie of pages.

It will save you lots of time.

3.2. Calling Parameters

The script can be called using different parameters :

  • --forced : script will not wait for you to press Start Scan button. Scanning will start immediately. It will end after scanning the first document(s). Without this parameter, the script will wait for you to press the Start Scan button on the scanner before starting the job.
  • --dpi=xxx : scan will be done using the DPI passed (75, 150, 300, ...) as parameter. You won't see the dialog box asking you ta select the resolution.
  • --title="abcdef" : dialog window title
  • --ipaddress=xx.xx.xx.xx : IP address of multifunction printer
  • --directory="/abc/def" : Directory where to store the scanned documents
  • --extension=abc : Extension of documents to generate (jpg, png, pdf, ...)

Most default values are defined in the script itself.

If the script is called without any parameter, it will show the resolution dialog box and it will run as a Scan-to-Folder server, scanning documents every time you press Start Scan button.

3.3. scan2folder.sh

Just create the script scan2folder.sh inside your home directory. To avoid any problem, make it hidden.

# gedit $HOME/.scan2folder.sh
# chmod +x $HOME/.scan2folder.sh

$HOME/.scan2folder.sh

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

# ----------------------------------------------------
#  User defined default parameters
#  Please, do not modify anything out of this section
# ----------------------------------------------------

# dialog box title
TITLE="HP MFP 1312 - Scan to folder"

# Multifunction printer IP address
IPADDRESS="192.168.xxx.xxx"

# Folder where to store scanned documents
DIRECTORY="$HOME/Pictures"

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

# ----------------------------------------------------
#  Handle command line parameters
# ----------------------------------------------------

# by default, scan is not forced (Scan will start when button pressed)
FORCED="0"

# loop to anayse all parameters, one by one
for VALUE in "$@"
do
  # get current parameter
  PARAMETER=`echo $VALUE | cut -d"=" -f1`

  # handle different parameters
  case $PARAMETER in
    "--forced")    FORCED="1" ;;
    "--dpi")       DPI=`echo $VALUE | cut -d"=" -f2` ;;
    "--title")     TITLE=`echo $VALUE | cut -d"=" -f2` ;;
    "--ipaddress") IPADDRESS=`echo $VALUE | cut -d"=" -f2` ;;
    "--directory") DIRECTORY=`echo $VALUE | cut -d"=" -f2` ;;
    "--extension") EXTENSION=`echo $VALUE | cut -d"=" -f2` ;;
    *) ;;             
  esac
done

# ----------------------------------------------------
#  Scanner resolution selection (in dpi)
# ----------------------------------------------------

# if resolution has not been given as parameter, display selection dialog box
if [ "$DPI" = "" ]
then
  # set TEXT according to selected method
  TEXT="\nSelect the resolution of scanned documents.\n"

  # ask for the scan resolution
  DPI=`yad --width=400 --height=220 --title="$TITLE" --text="$TEXT" --list --radiolist --column "Choice" --column "Resolution" --column "Size" FALSE 75 Small TRUE 150 Middle FALSE 300 Big FALSE 600 Huge | cut -d"|" -f2`
fi

# ----------------------------------------------------
#  Scan-to-Folder loop
# ----------------------------------------------------

# set initial text for the scan progression dialog box
TEXT=" Documents will be scanned using $DPI dpi \n They will be available under $DIRECTORY"

# infinite loop (end with CLOSE button or exit)
NBRDOCS="0"
while [ "$DPI" != "" ]
do    
  # display progression message
  echo "# $NBRDOCS document(s) scanned"

  # check if scan is forced or if scan button is pushed
  if [ "$FORCED" = "1" ]
    then STARTSCAN="1"
    else STARTSCAN=`wget --quiet -O - http://$IPADDRESS/hp/device/notifications.xml | grep "<StartScan>" | sed "s/^.*<StartScan>\([01]\)<\/StartScan>.*$/\1/g"`
  fi

  # if scan button has been pressed or scan is forced
  if [ "$STARTSCAN" = "1" ]
  then
    # check if documents are placed in Auto Feed
    AUTOFEED=`wget --quiet -O - http://$IPADDRESS/hp/device/notifications.xml | grep "<ADFLoaded>" | tail --lines=1 | sed "s/^.*<ADFLoaded>\([01]\)<\/ADFLoaded>.*$/\1/g"`

    # Document scan from the glass window
    if [ "$AUTOFEED" = "0" ]
    then
      # generate timestamped filename
      FILE=`date '+Scan_%Y%b%d-%Ih%Mm%Ss'`

      # display scan message
      echo "# Scanning document from glass window ..."

      # scan image with needed resolution
      scanimage --format=pnm --mode=Color --resolution=$DPI > "$DIRECTORY/$FILE.pnm"

      # display conversion message
      echo "# Converting the document ..."

      # convert to the proper format
      mogrify -format $EXTENSION "$DIRECTORY/$FILE.pnm"
      rm "$DIRECTORY/$FILE.pnm"

      # One document scanned
      NUMBER="1"

    # Document(s) scan from Auto Document Feed
    else
      # generate timestamped filename base
      FILE=`date '+Scan_%Y%b%d-%Ih%Mm%Ss_page'`

      # display scan message
      echo "# Scanning document(s) from auto feed ..."

      # scan all documents in the Automatic Document Feed
      NUMBER=`scanimage --format=pnm --mode=Color --resolution=$DPI --batch="$DIRECTORY/$FILE%d.pnm" --batch-start=1 --source ADF 2>&1 | grep "Scanned" | sed 's/^.*page \([0-9]*\).*$/\1/g' | tail --lines=1`

      # display conversion message
      echo "# Converting the $NUMBER scanned document(s) ..."

      # convert documents to the proper format
      mogrify -format $EXTENSION "$DIRECTORY/$FILE*.pnm"
      rm "$DIRECTORY/$FILE*.pnm"
    fi

    # update total of scanned documents
    NBRDOCS=`expr $NBRDOCS + $NUMBER`

    # if scan was forced, as scan has been done, set DPI to none to exit the loop
    if [ "$FORCED" = "1" ]; then DPI=""; fi

  # else, wait for 1 second before checking back the scanner
  else
    # wait for 1 second
    sleep 1
  fi
done | yad --width=500 --height=25 --progress --pulsate --auto-close --title "$TITLE" --text "$TEXT"

Next thing to do is to setup your own default parameters in the script :

  • TITLE
  • IPADDRESS
  • DIRECTORY
  • EXTENSION

Once done, you can test the script :

# ./.scan2folder --dpi=75 --forced

A scan should start immediately. Resulting file should be available in the default directory.

# ./.scan2folder

A dialog box should ask you whatever resolution you want to use.

Then, a scan should start every time you press Start Scan button. Resulting files should be available in the default directory.

3.4. Gnome Menus Declaration

Now that the script is up and running, you can declare some menu entries in the Applications / Graphics menu.

Typical command will be :

  • One click scan : $HOME/.scan2folder.sh –forced
  • One click scan (300 dpi) : $HOME/.scan2folder.sh –forced –dpi=300
  • Scan-to-Folder server : $HOME/.scan2folder.sh

You can now start your scan jobs directly from your Gnome menus.

 

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