/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
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
#!/usr/bin/env bash

####
# FILE NAME pdf2images.sh
# 
# Authors:
#    Gustav Hartvigsson 2024
#    Distributed under the Cool Licence 1.1
#
# Changes:
#
#  2024-07-10
#    * Initial version
#    *
####

__FILE=""
__FIRST_PAGE=0
__LAST_PAGE=0
__SCALE=250
__SANITY=true
__HAS_OPTIPNG=false

__SCRIPT_ROOT=$(dirname $(readlink -f $0))
source $__SCRIPT_ROOT/useful.inc.sh

function __usage () {
  echo "pdf2images.sh --- Convert a range of pdf pages into pngs."
  echo "USAGE:"
  echo "    pdf2images.sh <file>.pdf <first page> <last page>"
  echo ""
}

function __sanity_check () {
  # Check that we have the tools needed.
  __find_tool pdftoppm

  __silent which optipng
  if [ $? -gt 0 ]; then
    echo "    Can't find tool \"optpng\" (Not required)."
    __HAS_OPTIPNG=false
  fi
  
  if [[ $__SANITY == false ]]; then
    echo ""
    echo "Please install the missing tools."
    echo ""
    exit 1
  fi
}

function __process () {

  pdftoppm -f $__FIRST\
           -l $__LAST\
           -r $__SCALE\
           -gray\
           -png\
           -progress\
           $__FILE\
           ${__FILE%%.*}

  if [[ $__HAS_OPTIPNG == true ]]; then
    optipng ${__FILE%%.*}*.png
  fi
}

function __parse_args () {
  if [ $# -eq 0 ]; then
    __usage
    exit 1
  fi
  
  __FILE=$1
  shift
  __FIRST=$1
  shift
  __LAST=$1
  shift

  if [ $# -ne 0 ]; then
    echo "Nummber of arguments missmatch."
    echo ""
    __usage
    exit 1
  fi
}

function __main () {
  __sanity_check
  __parse_args "${@}"
  __process
  exit 0
}

__main "${@}"