Class: PdfParadise::RotatePdfFile

Inherits:
Base
  • Object
show all
Defined in:
lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb

Overview

PdfParadise::RotatePdfFile

Constant Summary collapse

ROTATE_BY_N_DEGREES =
#

ROTATE_BY_N_DEGREES

#
90
ROTATE_REGEX =
#

ROTATE_REGEX

#
/(--rotate (\d+))/

Constants inherited from Base

Base::NAMESPACE

Instance Method Summary collapse

Methods inherited from Base

#basename, #be_verbose?, #change_directory, #commandline_arguments?, #copy_file, #delete_file, #e, #ecomment, #esystem, #first_argument?, #gold, #infer_the_namespace, #input_without_leading_hyphens?, #internal_hash?, #is_an_image_file?, #is_on_roebe?, #lightsteelblue, #log_dir?, #mkdir, #mv, #n_pages?, #namespace?, #no_file_at, #opne, #orange, #reset_the_internal_hash, #return_commandline_arguments_starting_with_hyphens, #return_files_from_the_commandline_arguments, #return_pwd, #rev, #set_be_quiet, #set_commandline_arguments, #steelblue, #try_to_ensure_that_this_directory_exists, #write_what_into

Constructor Details

#initialize(i = nil, run_already = true) ⇒ RotatePdfFile

#

initialize

#


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 37

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  case i
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    i = nil
    run_already = false
  end
  set_commandline_arguments(i)
  set_input(
    i.reject {|entry|
      entry.start_with?('--')
    }
  )
  case run_already
  # ======================================================================= #
  # === :run_already
  # ======================================================================= #
  when :run_already
    run_already = true
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Instance Method Details

#add_qualityObject

#

add_quality

#


183
184
185
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 183

def add_quality
  return '-quality 100 '
end

#do_convert_this_pdf_file(i, be_verbose = true) ⇒ Object Also known as: do_rotate_the_pdf_file

#

do_convert_this_pdf_file

#


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 235

def do_convert_this_pdf_file(
    i, be_verbose = true
  )
  i = i.to_s
  output_file = output_file?
  # ======================================================================= #
  # Use convert by default.
  # ======================================================================= #
  _ = @use_this_command.to_s
  _ = _.dup
  if _.start_with? 'qpdf'
    _ << ' --rotate='+rotate_by_n_degrees?+' '
    if @overwrite_the_original_file
      # =================================================================== #
      # --replace-input may be given in place of outfile.
      # =================================================================== #
      _ << ' --replace-input'
    end
  else
    _ << ' -rotate '+rotate_by_n_degrees?+' '
  end
  _ << add_quality if _.start_with? 'convert'
  _ << " #{i}" # Here we add the input-file.
  unless _.include? '--replace-input'
    _ << " #{output_file}" # Add the output file here.
  end
  _.rstrip!
  _.squeeze!(' ')
  case be_verbose
  # ======================================================================= #
  # === :be_quiet
  # ======================================================================= #
  when :be_quiet
    be_verbose = false
  end
  if be_verbose
    esystem _
  else
    system _
  end
  # ======================================================================= #
  # Do cleanup if qpdf was used.
  # ======================================================================= #
  if _.include?('--replace-input') and qpdf_was_used?
    delete_these_files = Dir['*.pdf.~qpdf-orig']
    delete_these_files.each {|this_file|
      delete_this_file(this_file)
    }
  end 
  if File.exist?(output_file) and is_on_roebe? and be_verbose
    require 'open'
    Open[output_file] # Open the .pdf file here on a roebe system.
  end
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


150
151
152
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 150

def input?
  @input
end
#

menu

#


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 200

def menu(
    i = @commandline_arguments
  )
  if i.is_a? Array
    i.each {|entry|
      menu(entry)
    }
  else
    case i
    # ===================================================================== #
    # === --help
    #
    # Since as of February 2023, empty input, aka '', will no longer call
    # this method.
    # ===================================================================== #
    when /-?-?help$/i # '' means empty.
      show_help
      exit
    # ===================================================================== #
    # === --rotate=5
    #
    # Usage example:
    #
    #   rpdf 13.06.1991_Scan_EVVA_Schließplan_für_Zylinder_Schließanlagen.pdf --rotate=1
    #
    # ===================================================================== #
    when /-?-?rotate=(\d{1,5})$/i
      set_rotate_level($1.to_s)
    end
  end
end

#notify_the_user_that_the_conversion_has_finishedObject

#

notify_the_user_that_the_conversion_has_finished

#


190
191
192
193
194
195
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 190

def notify_the_user_that_the_conversion_has_finished
  e 'Finished converting! The file should now reside at:'
  e
  e sfile("  #{output_file?}")
  e
end

#opnnObject

#

opnn

#


169
170
171
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 169

def opnn
  Opn.opn(namespace: namespace?)
end

#output_file?(i = input? ) ⇒ Boolean

#

output_file?

#

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 157

def output_file?(
    i = input?
  )
  return i if @overwrite_the_original_file # Early return.
  _ = basename(i).delete_suffix('.pdf')
  _.tr(' ','') if _.include? ' '
  return "rotated_pdf_file_#{_}.pdf"
end

#qpdf_was_used?Boolean

#

qpdf_was_used?

#

Returns:

  • (Boolean)


92
93
94
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 92

def qpdf_was_used?
  @use_this_command == :qpdf
end

#resetObject

#

reset (reset tag)

#


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 74

def reset
  infer_the_namespace
  # ======================================================================= #
  # === @use_this_command
  # ======================================================================= #
  @use_this_command = :qpdf # :convert
  set_rotate # Set the default value, after @use_this_command was set.
  # ======================================================================= #
  # === @overwrite_the_original_file
  #
  # Other users may want to set the next variable to false.
  # ======================================================================= #
  @overwrite_the_original_file = true
end

#rotate_by_n_degrees?Boolean Also known as: rotate?

#

rotate_by_n_degrees?

#

Returns:

  • (Boolean)


176
177
178
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 176

def rotate_by_n_degrees?
  @rotate_by_n_degrees
end

#runObject

#

run (run tag)

#


293
294
295
296
297
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 293

def run
  menu
  do_convert_this_pdf_file(input?)
  notify_the_user_that_the_conversion_has_finished
end

#set_input(i = '') ⇒ Object

#

set_input

#


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 110

def set_input(i = '')
  i = i.join(' ').strip if i.is_a? Array
  i = i.to_s.dup
  if i.start_with? 'file://'
    i.sub!(/^file:\/\//,'')
  end
  case i
  # ======================================================================= #
  # We add support for --rotate next. When there is a match, we
  # will get rid of that part of the input.
  # ======================================================================= #
  when ROTATE_REGEX, /--rotate/ # Usage: rotate --rotate 100 5-Kapitel_5_AllgMikrobiologie_SS2015_students_2S.pdf
    i =~ ROTATE_REGEX
    set_rotate($2.to_s.dup)
    i.gsub!(/#{$1.to_s}/, '')
  end unless File.exist? i
  i = i.to_s.strip
  @input = i
end

#set_rotate(i = ROTATE_BY_N_DEGREES) ⇒ Object Also known as: set_rotate_level

#

set_rotate

#


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 133

def set_rotate(
    i = ROTATE_BY_N_DEGREES
  )
  i = i.to_i
  case @use_this_command
  # ======================================================================= #
  # === :qpdf
  # ======================================================================= #
  when :qpdf
    i = "+#{i}" if i > 0
  end
  @rotate_by_n_degrees = i.to_s.dup # Always .dup it, just in case.
end

#show_helpObject

#

show_help (help tag)

#


99
100
101
102
103
104
105
# File 'lib/pdf_paradise/utility_scripts/rotate_pdf_file.rb', line 99

def show_help
  opne 'The available options are:'
  e
  eparse '  --rotate N # rotate by n degrees (into '\
         'clockwise direction. Example: rotatepdf --rotate 90 foo.pdf)'
  e
end