/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1

« back to all changes in this revision

Viewing changes to scripts/pdf2images.sh

  • Committer: Gustav Hartvigsson
  • Date: 2024-07-10 19:39:45 UTC
  • Revision ID: git-v1:e890a786f7aab410893526606b5c10a866eb425a
* pdf2images.sh
  - general cleanup
  - fix error with arg parsing (can't arguments via variabel.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env bash
2
2
 
3
 
___ARGS=$@
4
 
 
5
 
___FILE=""
6
 
___FIRST_PAGE=0
7
 
___LAST_PAGE=0
8
 
___SANITY=1
9
 
___SCALE=250
10
 
___HAS_OPTIPNG=1
11
 
 
12
 
function ___usage () {
 
3
####
 
4
# FILE NAME pdf2images.sh
 
5
 
6
# Changes:
 
7
#
 
8
#  2024-07-10
 
9
#    * Initial version
 
10
#    *
 
11
####
 
12
 
 
13
__FILE=""
 
14
__FIRST_PAGE=0
 
15
__LAST_PAGE=0
 
16
__SCALE=250
 
17
__SANITY=true
 
18
__HAS_OPTIPNG=false
 
19
 
 
20
function __usage () {
13
21
  echo "pdf2images.sh --- Convert a range of pdf pages into pngs."
14
22
  echo "USAGE:"
15
23
  echo "    pdf2images.sh <file>.pdf <first page> <last page>"
16
24
  echo ""
17
25
}
18
26
 
19
 
function ___silent () {
 
27
function __silent () {
20
28
  $@ >> /dev/null 2>&1
21
29
  return $?
22
30
}
23
31
 
24
 
function ___sanity_check () {
 
32
function __sanity_check () {
25
33
  # Check that we have the tools needed.
26
 
  ___silent which pdftoppm 
 
34
  __silent which pdftoppm 
27
35
 
28
36
  if [ $? -gt 0 ]; then
29
37
    echo "    Can't find tool \"pdftoppm\" (Required)."
30
 
    ___SANITY=0
 
38
    __SANITY=false
31
39
  fi
32
40
 
33
 
  ___silent which optipng
 
41
  __silent which optipng
34
42
  if [ $? -gt 0 ]; then
35
43
    echo "    Can't find tool \"optpng\" (Not required)."
36
 
    ___HAS_OPTIPNG=0
 
44
    __HAS_OPTIPNG=false
37
45
  fi
38
46
  
39
 
  if [ $___SANITY -eq 0 ]; then
 
47
  if [[ $__SANITY == false ]]; then
40
48
    echo "Please install the missing tools."
41
49
    echo ""
42
50
    exit 1
43
51
  fi
44
52
}
45
53
 
46
 
function ___process () {
 
54
function __process () {
47
55
 
48
 
  pdftoppm -f $___FIRST\
49
 
           -l $___LAST\
50
 
           -r $___SCALE\
 
56
  pdftoppm -f $__FIRST\
 
57
           -l $__LAST\
 
58
           -r $__SCALE\
51
59
           -gray\
52
60
           -png\
53
61
           -progress\
54
 
           $___FILE\
55
 
           ${___FILE%%.*}
 
62
           $__FILE\
 
63
           ${__FILE%%.*}
56
64
 
57
 
  if [ $___HAS_OPTIPNG -eq 1 ]; then
58
 
    optipng ${___FILE%%.*}*.png
 
65
  if [[ $__HAS_OPTIPNG == true ]]; then
 
66
    optipng ${__FILE%%.*}*.png
59
67
  fi
60
68
}
61
69
 
62
 
function ___parse_args () {
 
70
function __parse_args () {
63
71
  if [ $# -eq 0 ]; then
64
 
    ___usage
 
72
    __usage
65
73
    exit 1
66
74
  fi
67
75
  
68
 
  ___FILE=$1
69
 
  shift
70
 
  ___FIRST=$1
71
 
  shift
72
 
  ___LAST=$1
 
76
  __FILE=$1
 
77
  shift
 
78
  __FIRST=$1
 
79
  shift
 
80
  __LAST=$1
73
81
  shift
74
82
 
75
83
  if [ $# -ne 0 ]; then
76
84
    echo "Nummber of arguments missmatch."
77
85
    echo ""
78
 
    ___usage
 
86
    __usage
79
87
    exit 1
80
88
  fi
81
89
}
82
90
 
83
 
function ___main () {
84
 
  ___sanity_check
85
 
  ___parse_args $___ARGS
86
 
  ___process
 
91
function __main () {
 
92
  __sanity_check
 
93
  __parse_args "${@}"
 
94
  __process
87
95
  exit 0
88
96
}
89
97
 
90
 
___main
 
98
__main "${@}"
91
99