/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 20:03:08 UTC
  • Revision ID: git-v1:d55384577b22b2372b807d0db71abc00396b803a
* enc2firefox.sh
  - Simpelfied if-else-statement.

Show diffs side-by-side

added added

removed removed

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