-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfview.sh
executable file
·110 lines (86 loc) · 2.03 KB
/
pdfview.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
#
# pdfview.sh - displays the text in a PDF; relies on pdftotext from
# xpdf (https://www.xpdfreader.com/pdftotext-man.html)
# or poppler (https://github.com/freedesktop/poppler)
#
# Copyright (c) 2022 Sriranga Veeraraghavan <ranga@calalum.org>. All
# rights reserved. See LICENSE.txt.
#
# v.0.1.0 - initial version
# v.0.1.1 - fixes for debian
# v.0.1.2 - fixes based on shellcheck
#
# default columns to for wrapping
COLS=75
# default programs
PGM_PDF2TXT=pdftotext
PGM_PDF2TXT_OPTS="-layout -nopgbrk"
PGM_FMT="fmt"
PGM_FMT_OPTS="-s"
PGM_FOLD="fold"
PGM_FOLD_OPTS="-s"
# global variables
FILE=
FORMATTER=
FORMATTER_OPTS=
# global functions
printUsage()
{
echo "Usage: $0 [-c cols] [file]" 1>&2;
}
printError()
{
echo "ERROR: " "$@" 1>&2;
}
# main
# check if the first argument is -c, and if so check
# if a valid number of columns were specified
if [ X"$1" = X"-c" ] ; then
shift ;
if [ X"$1" = "X" ] ; then
printUsage;
exit 1;
fi
if test "$1" -ge 0 2>/dev/null ; then
COLS="$1" ;
shift;
else
printError "'$1' not a number."
exit 1;
fi
fi
# check to see if a file is provided
if [ X"$1" = "X" ] ; then
printUsage;
exit 1;
fi
FILE="$1"
if [ ! -r "$FILE" ] ; then
printError "'$FILE' does not exist, or is not readable." ;
exit 1;
fi
# check to see if pdftotext is available
if type "$PGM_PDF2TXT" > /dev/null 2>&1 ; then
:
else
printError "$PGM_PDF2TXT not found." ;
exit 1;
fi
# determine which formatting program to use
if type "$PGM_FMT" > /dev/null 2>&1 ; then
FORMATTER="$PGM_FMT" ;
FORMATTER_OPTS="$PGM_FMT_OPTS -w $COLS" ;
elif type "$PGM_FOLD" > /dev/null 2>&1 ; then
FORMATTER="$PGM_FOLD" ;
FORMATTER_OPTS="$PGM_FOLD_OPTS -w $COLS" ;
fi
# if a formatter is available, post process the output
# of pdftotext with it
if [ X"$FORMATTER" != "X" ] ; then
"$PGM_PDF2TXT" $PGM_PDF2TXT_OPTS "$FILE" - | \
LC_ALL=C "$FORMATTER" $FORMATTER_OPTS
else
"$PGM_PDF2TXT" $PGM_PDF2TXT_OPTS "$FILE" -
fi
exit $?