Admin Stuff

This is not straight forward. I am using the fax software from http://www.ip-phone-forum.de/showthread.php?t=145110. I placed the CapiSpFax utitlity on an internal webserver I have running. Sending a fax now consists of two steps. First I need to create the correct tiff file. I was only able to send normal resolution faxes and only if the page has an exact size of 1728x1145 pixels. I already have setup my scanner to scan an A4 page to a pdf file directly. To send this file via fax I use.

 $ gs -sDEVICE=pgmraw -r209x98 -g1728x1145 -sOutputFile=- \
           -dSAFER -dNOPAUSE -dBATCH -dNOINTERPOLATE -q <pdf-file> | \
       pgmtopbm -threshold -value 0.9 | \
       pnmtotiff -g3 - > <webserver-dir>/fax.tiff


The resolution value is not exactly what is defined for fax but this resolution will exactly place an A4 page into the given image size.

<webserver-dir> is the internal web-servers webroot. To send the file from the FritzBox, I telnet to the box and then execute:

 # cd /var
 # wget http://<internal-server>/CapiSpFax
 # chmod 0755 ./CapiSpFax
 # wget http://<internal-server>/fax.tiff
 # ./CapiSpFax -v -l "<meine-fax-id>" -o "<meine-msn>" -d "<ziel-fax-nummer>" -t fax.tiff


Since all this is quite tedious, I wrote some scripts to simplify it. The first is a utility to convert a PDF (or PS) file to a multi-page Fax-tiff-G3 file:

#!/bin/sh -e

OPTS=`getopt -o hbl -n pdftofax -- "$@"`
if [ $? != 0 ] ; then exit 1 ; fi
eval set -- "$OPTS"

pbmopts=""
rotate="cat"
res="200x98"
size="1728x1145"

while true; do
    case "$1" in
        -h) cat <<EOF
Usage: pdftofax [-b] [-l] [-h] <[pdf-file] >[tiff-file]

Reads a PDF file on standard input and generates a multi-page tiff
file in Fax-G3 encoding on standard output with the correct
(asymetric) resolution.

    -h  Show this help message

    -b  Generate a non-dithered binary tiff. Use this for scanned
        documents

    -l  Landscape mode. The pages will be rotated by 90 degrees
        counter-clock-wise

EOF
            exit 0
            ;;
        -b)
            pbmopts="-threshold -value 0.9"
            shift
            ;;
        -l)
            rotate="pnmflip -ccw"
            res="98x200"
            size="1145x1728"
            shift
            ;;
        --)
            shift ; break ;;
        *)
            echo "Internal error"; exit 1 ;;
    esac
done

temp=/tmp/pdftofax-$$
mkdir "$temp"
trap 'cd /; rm -rf "$temp"' 0 1 2 15
cd "$temp"

gs -sDEVICE=pgmraw -r${res} -g${size} -sOutputFile="%d.pgm" -dSAFER -dNOPAUSE -dBATCH -dNOINTERPOLATE -q -

for page in *.pgm; do
    pgmtopbm $pbmopts <$page \
        | ${rotate} \
        | pnmtotiff -g3 - >${page%.pgm}.tiff
done

tiffcp *.tiff fax.tiff
cat fax.tiff

And next a python script which will transfer a fax to the fritzbox and send it there

#!/usr/bin/python

fritzhost  = '...'      # Hostname of the fritzbox to telnet to
fritzpwkey = '...'      # Key in the kwallet 'Passwords' group under which the fritzbox password is stored
webdir     = '...'      # Local (possible NFS) directory of the internal webserver
weburl     = '...'      # URL of the internal webserver from the fritzbox
webvhost   = '...'      # Hostname under which the internal webserver is found (vhost)
myfaxid    = '...'      # My own fax ID
mymsn      = '...'      # The MSN to send the fax with

import telnetlib
import pcop
import pydcop
import shutil
import sys

class FritzTelnet:

    def __init__(self, fritzhost, password):
        self.telnet = telnetlib.Telnet(fritzhost)
        self.telnet.read_until('password:')
        self.telnet.write(password+'\n')
        self.telnet.read_until('# ')

    def run(self,command):
        self.telnet.write(command+'\n')
        return self.telnet.read_until('# ')

if len(sys.argv) != 2:
    print """Usage: fritzfax [faxnr] <[faxfile]

where [faxnr] is the destination number to send the fax tu and
[faxfile] is a Fax-G3 encoded tiff image file.
"""
    sys.exit(1)

faxnr = sys.argv[1]

shutil.copyfileobj(sys.stdin, file('%s/fax.tiff' % webdir,"w"))

kded = pydcop.anyAppCalled('kded')
walletid = kded.kwalletd.open('kdewallet',0)
fritzpw = kded.kwalletd.readPassword(walletid, 'Passwords', fritzpwkey)
kded.kwalletd.close(walletid,0)

fritz = FritzTelnet(fritzhost, fritzpw)

fritz.run("cd /var")
fritz.run("rm -f CapiSpFax")
fritz.run("wget --header 'Host: %s' '%s/CapiSpFax'" % (webvhost, weburl))
fritz.run("chmod 755 ./CapiSpFax")
fritz.run("rm -f fax.tiff")
fritz.run("wget --header 'Host: %s' '%s/fax.tiff'" % (webvhost, weburl))
print fritz.run("./CapiSpFax -v -l '%s' -o '%s' -d '%s' -t fax.tiff"
                % (myfaxid, mymsn, faxnr))
fritz.run("rm -f CapiSpFax fax.tiff")
os.unlink('%s/fax.tiff' % webdir)

This script works by copying the fax file to a local webserver and then telneting to the fritzbox, downloading the CapiSpFax tool and the fax from the local webserver and then sending the fax.