/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:52:28 UTC
  • mfrom: (22.1.1)
  • Revision ID: git-v1:924d562846d864ade91a0ba932977a58e380ca6f
Merge.

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