/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
22 by Gustav Hartvigsson
* useful.inc.sh
1
#!/usr/bin/env bash
2
3
####
4
# FILE NAME pdf2images.sh
5
# 
27 by Gustav Hartvigsson
Added copyright information to all files.
6
# Authors:
7
#    Gustav Hartvigsson 2024
8
#    Distributed under the Cool Licence 1.1
9
#
22 by Gustav Hartvigsson
* useful.inc.sh
10
# Changes:
11
#
12
#  2024-07-10
13
#    * Initial version
14
#    *
15
####
16
17
__FILE=""
18
__FIRST_PAGE=0
19
__LAST_PAGE=0
20
__SCALE=250
21
__SANITY=true
22
__HAS_OPTIPNG=false
23
24
__SCRIPT_ROOT=$(dirname $(readlink -f $0))
25
source $__SCRIPT_ROOT/useful.inc.sh
26
27
function __usage () {
28
  echo "pdf2images.sh --- Convert a range of pdf pages into pngs."
29
  echo "USAGE:"
30
  echo "    pdf2images.sh <file>.pdf <first page> <last page>"
31
  echo ""
32
}
33
34
function __sanity_check () {
35
  # Check that we have the tools needed.
36
  __find_tool pdftoppm
37
38
  __silent which optipng
39
  if [ $? -gt 0 ]; then
40
    echo "    Can't find tool \"optpng\" (Not required)."
41
    __HAS_OPTIPNG=false
42
  fi
43
  
44
  if [[ $__SANITY == false ]]; then
45
    echo ""
46
    echo "Please install the missing tools."
47
    echo ""
48
    exit 1
49
  fi
50
}
51
52
function __process () {
53
54
  pdftoppm -f $__FIRST\
55
           -l $__LAST\
56
           -r $__SCALE\
57
           -gray\
58
           -png\
59
           -progress\
60
           $__FILE\
61
           ${__FILE%%.*}
62
63
  if [[ $__HAS_OPTIPNG == true ]]; then
64
    optipng ${__FILE%%.*}*.png
65
  fi
66
}
67
68
function __parse_args () {
69
  if [ $# -eq 0 ]; then
70
    __usage
71
    exit 1
72
  fi
73
  
74
  __FILE=$1
75
  shift
76
  __FIRST=$1
77
  shift
78
  __LAST=$1
79
  shift
80
81
  if [ $# -ne 0 ]; then
82
    echo "Nummber of arguments missmatch."
83
    echo ""
84
    __usage
85
    exit 1
86
  fi
87
}
88
89
function __main () {
90
  __sanity_check
91
  __parse_args "${@}"
92
  __process
93
  exit 0
94
}
95
96
__main "${@}"
97