/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 14:25:13 UTC
  • Revision ID: git-v1:55fc68b13933a25315ad96a4c69e64d962d2c2d4
Fixed install script's --target flag.

Show diffs side-by-side

added added

removed removed

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