/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-21 15:48:36 UTC
  • Revision ID: git-v1:4a62dae0b144b55ac695b792b1b3c3c504a008eb
[do-offline-updates.sh] Rewrite to be more inline with the rest of the tools.

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