-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdftoebook
executable file
·73 lines (61 loc) · 1.64 KB
/
pdftoebook
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
#!/bin/sh
#
# reformat a pdf to a smaller page by extracting text and compiling with groff
#
# -h for inline help
#
# to select only a region of each page, either:
# pdftoebook -p '-m 2 -b [2,3-460,682]' file.pdf
# pdftoebook -p '-m 3 -b [2,3-460,682]' file.pdf
# to obtain the coordinates, press key 'b' in hovacui to save the current box
# in hovacui-boxes.txt; enlarge slightly (~5.0) since only characters that fall
# completely in the box are included, and the current box may lack for example
# tall letters in the first line
# options
WIDTH=200
HEIGHT=250
MARGIN=5
RECUR=-n
while getopts "?w:h:m:p:" opt;
do
case $opt in
w) WIDTH=$OPTARG;;
h) HEIGHT=$OPTARG;;
m) MARGIN=$OPTARG;;
n) RECUR="";;
p) CONVOPTS="$OPTARG";;
*) echo -en "usage:\n\tpdftoebook [-w width] [-h height] "
echo -e "[-m margin] in.pdf [out.pdf]"
echo -e "\t\t-w width\twidth in points, default 200"
echo -e "\t\t-w height\theight in points, default 250"
echo -e "\t\t-w margin\tmargin in points, default 5"
echo -e "\t\t-p options\tpass options to pdftoroff"
exit 1;;
esac
done
# input and output file names
shift $((OPTIND - 1))
[ $# -lt 1 ] && echo "pdf file missing" && exit 1
IN="$1"
shift 1
OUT=$(basename "$IN" .pdf)-ebook.pdf
[ $# -gt 0 ] && OUT="$1"
echo "$IN -> $OUT ${WIDTH}x$HEIGHT +$MARGIN"
# calculate line and page
LINE=$(($WIDTH-$MARGIN*2))
PAGE=$(($HEIGHT-$MARGIN*2))
# extract text from pdf and recompile with groff
{
cat <<!
.\" set margins and line and page lenght
.device papersize=${WIDTH}p,${HEIGHT}p
.po ${MARGIN}p
.ll ${LINE}p
.pl ${PAGE}p
.
.\" font family (Helvetica)
.fam H
!
pdftoroff $RECUR $CONVOPTS "$IN";
} | \
groff -Dutf8 -Tpdf - > "$OUT"