-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathimagedir2pdf
executable file
·89 lines (72 loc) · 1.69 KB
/
imagedir2pdf
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
#!/bin/bash
#
# imagedir2pdf : Convert a directory of images to a single PDF
#
#
# Version 0.1 (2011/03/03)
# (c) 2011 Mathieu Comandon <[email protected]>
# Licensed under the terms of the GPL Version 3
#
set -e
usage()
{
cat << EOF
Usage: $0 [OPTION]... destination
Convert a directory of images to a single PDF file
OPTIONS:
-i, --input Directory to convert (Default: current directory)
-m, --method Method used to convert to PDF (convert or sam2p, default to convert)
EOF
}
# Parse arguments
if [ "$#" -eq 0 ] ; then
usage
exit 2
fi
PARAMS=$(getopt -n $0 -o i:m:h --long input:,method:help -- "$@")
eval set -- "$PARAMS"
while true ; do
case "$1" in
-i|--input) input_dir=$2 ; shift 2 ;;
-m|--method) method=$2 ; shift 2 ;;
--) dest_dir=$2 ; shift ; break ;;
-h|--help) usage ; exit 2 ;;
*) usage ; exit 2 ;;
esac
done
if [ ! -d "$dest_dir" ] ; then
echo "$dest_dir is not valid destination!"
exit 2
fi
if [ -z "$dest_dir" ] ; then
echo "You must specify an output dir!"
exit 2
fi
if [ "$method" != "sam2p" ] ; then
method="convert"
fi
if [ -z $(which $method) ] ; then
echo "You must install $method to continue "
exit 2
fi
if [ -z $(which pdftk) ] ; then
echo "You must install pdftk to continue "
exit 2
fi
if [ -z "$input_dir" ] ; then
input_dir=$PWD
fi
pdfname=$(basename $input_dir)
oldpwd=$PWD
cd $input_dir
IFS=$(echo -en "\n\b")
shopt -s extglob
temp_dir=$(mktemp -d)
index=1
for image in $(ls *(*.jpg|*.png)) ; do
$method $image $temp_dir/page$(printf "%03d" $index).pdf
let index=$index+1
done
pdftk ${temp_dir}/*.pdf cat output ${dest_dir}/${pdfname}.pdf
rm -r $temp_dir
cd $oldpwd