aboutsummarylogtreecommitdiffstats
path: root/psfilter
blob: 8efcf17cac483921257f3e80ad72a4d8a59a768a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
#
# psfilter.simple -- Convert pdf and text to postscript.  Control of
# duplexing for postscript files is via the insertion of a duplexing
# control line as the second line of the postscript file.  This seems
# to work for pdftops, pdf2ps, and cairo output.  It doesn't work for
# postscript files created by enscript.  Instead, enscript duplexing
# is controlled by a command line option in enscript.  Duplex on pdf
# files is controlled by a pdftops flag.
#
# Requires
#    poppler (xpdf): pdftops
#    enscript

set -u

# define temporary files
infile="/tmp/infile.$$"
tempfile="/tmp/tempfile.$$"
headfile="/tmp/headfile.$$"
tailfile="/tmp/tailfile.$$"

# choose your paper size
PAPER='Letter'
#PAPER='A4'

# define whether duplex is desired by uncommenting appropriate lines
#duplex='false'
duplex='true'
#duplexflag=''
duplexflag='-duplex'

# send the standard input to a temporary file
cat > "${infile}"

# figure out what type of file we are trying to print
fileinfo="$(file -b "${infile}")"

# do conversions to postscript based on file type and put in tempfile
case "${fileinfo}" in
*ASCII*)      enscript -DDuplex:"${duplex}" -M "${PAPER}" -o - "${infile}" ;;
*PDF*)        pdftops -paper 'match' "${duplexflag}" "${infile}" - ;;
*PostScript*)
  if [ "${duplex}" = 'true' ]; then
    # are we doing duplex?  if so, insert a special string in postscript file
    sed -e "1 w ${headfile}" -e "2,$ w ${tailfile}" < "${infile}" > /dev/null
    cat "${headfile}" > "${tempfile}"
    echo "<< /Duplex true >> setpagedevice" >> "${tempfile}"
    cat "${tailfile}" >> "${tempfile}"
  else
    # if not duplex, don't do anything
    cat "${tailfile}" >> "${tempfile}"
  fi
  # send the file to standard output
  cat "${tempfile}"
  ;;
*)
  echo "Cannot print file of type ${fileinfo}" | \
  enscript -M "${PAPER}" -DDuplex:"${duplex}" -o -
  ;;
esac

# clean up
rm -f "${infile}" "${tempfile}" "${headfile}" "${tailfile}"