Class: PdfParadise::DeleteTheFirstPageOfThisPdfFile

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

Overview

PdfParadise::DeleteTheFirstPageOfThisPdfFile

Constant Summary collapse

SHALL_WE_OVERWRITE_THE_ORIGINAL_PDF_FILE =
#

SHALL_WE_OVERWRITE_THE_ORIGINAL_PDF_FILE

Set this constant to false if you do not want to overwrite the original .pdf file.

#
true

Constants inherited from Base

Base::NAMESPACE

Class Method Summary collapse

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, #opnn, #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(commandline_arguments = nil, run_already = true, &block) ⇒ DeleteTheFirstPageOfThisPdfFile

#

initialize

#


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 54

def initialize(
    commandline_arguments = nil,
    run_already           = true,
    &block
  )
  reset
  case commandline_arguments
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    commandline_arguments = []
    run_already = false
  end
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      set_be_quiet
    end
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

PdfParadise::DeleteTheFirstPageOfThisPdfFile[]

#


393
394
395
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 393

def self.[](i = ARGV)
  new(i)
end

Instance Method Details

#array_backup_files?Boolean Also known as: backups?

#

array_backup_files?

#

Returns:

  • (Boolean)


146
147
148
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 146

def array_backup_files?
  @internal_hash[:array_backup_files]
end

#create_a_backup_file(entry, temp_dir = temp_dir? ) ⇒ Object

#

create_a_backup_file

#


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 203

def create_a_backup_file(
    entry,
    temp_dir = temp_dir?
  )
  e "#{rev}First, #{steelblue('a backup file will be created')}#{rev}."
  # ======================================================================= #
  # Next ensure that the temp-dir exists.
  # ======================================================================= #
  try_to_ensure_that_this_directory_exists(temp_dir)
  target = temp_dir+File.basename(entry)
  if File.exist? target # Delete the old .pdf copy here.
    delete_file(target)
  end
  copy_file(entry, target)
  if File.exist? target
    e "#{rev}A backup-file (a copy) now exists at `#{sfile(target)}#{rev}`."
    @internal_hash[:array_backup_files] << File.absolute_path(target)
  end
end

#determine_the_input_files(commandline_arguments = commandline_arguments? ) ⇒ Object

#

determine_the_input_files

This method will specifically select for all .pdf files.

#


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 168

def determine_the_input_files(
    commandline_arguments = commandline_arguments?
  )
  # ======================================================================= #
  # Next, if i is empty, and we are on Roebe, we will use the default
  # book, if a certain ENV variable exists AND if the file that points
  # to that location also exists.
  # ======================================================================= #
  if commandline_arguments.empty? and is_on_roebe?
    if ENV.has_key?('MAIN_BOOK') and
      File.exist?(ENV['MAIN_BOOK'])
      this_file = ENV['MAIN_BOOK'].dup
      e "#{rev}As we are on roebe, we will use this file:"
      e
      e sfile("  #{this_file}")
      e
      commandline_arguments << this_file
    end
  end
  commandline_arguments.map! {|entry|
    entry = entry.to_s unless entry.is_a?(String)
    entry = Dir[entry] if entry.include? '*'
    entry
  }
  commandline_arguments.flatten!
  _ = commandline_arguments.select {|entry|
    entry.end_with?('.pdf') and File.file?(entry) 
  }
  _.map! {|entry| File.absolute_path(entry) } if _ and !_.empty?
  @internal_hash[:work_on_these_files] = _
end

#do_delete_the_first_page_of_this_pdf_file(i, use_this_pdf_application = , be_verbose = be_verbose? ) ⇒ Object

#

do_delete_the_first_page_of_this_pdf_file

#


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 334

def do_delete_the_first_page_of_this_pdf_file(
    i,
    use_this_pdf_application = @internal_hash[:use_this_pdf_application],
    be_verbose               = be_verbose?
  )
  use_this_pdf_application = use_this_pdf_application.to_s
  # ======================================================================= #
  # Determine the name of the (new) output file next:
  # ======================================================================= #
  name_of_the_output_file = name_of_the_output_file?(i)
  if File.exist?(name_of_the_output_file) # Delete the output file first.
    delete_file(name_of_the_output_file)
  end
  if i.include? ' '
    i = '"'+i+'"'
  end
  # ======================================================================= #
  # Store how many pages this .pdf file has.
  # ======================================================================= #
  has_n_pages = PdfParadise.n_pages_in_this_pdf_file?(i)
  _ = ''.dup
  _ << use_this_pdf_application
  case _
  # ======================================================================= #
  # === Support for pdftk
  # ======================================================================= #
  when /pdftk/
    _ << ' '+i+' cat 2-'+(has_n_pages.to_i).to_s+' '+
         has_n_pages.to_s+'-end output '+
         name_of_the_output_file
  # ======================================================================= #
  # === Support for hexapdf
  # ======================================================================= #
  when /hexapdf/
    _ << ' modify '+i+' -i 2-e '+name_of_the_output_file
  # ======================================================================= #
  # === Support for qpdf
  # ======================================================================= #
  when /qpdf/
    _ << ' '+'--pages '+i+' 2-'+(has_n_pages.to_i).to_s+' -- '+
         i+' '+
         name_of_the_output_file
  else
    e 'Unknown pdf handler: '+steelblue(_)
    e 'Exiting.'
    exit
  end
  e "Deleting the first page of this .pdf file next: "\
    "#{sfile(i)}" if be_verbose
  # ======================================================================= #
  # Next, do the actual work as-such, via esystem() and a bit of padding:
  # ======================================================================= #
  e; e "  #{_}"; system(_); e
  return name_of_the_output_file
end

#do_work_on_these_pdf_files(i = work_on_these_files?, , temp_dir = temp_dir? ) ⇒ Object Also known as: process_each_pdf

#

do_work_on_these_pdf_files (main tag)

This is the method that will delete the first page of a given .pdf file.

#


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
289
290
291
292
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 237

def do_work_on_these_pdf_files(
    i        = work_on_these_files?,
    temp_dir = temp_dir?
  )
  i.each {|entry|
    opnn { :no_trailing }
    e; e
    e "#{rev}Working on the file #{sfile(entry)} #{rev}next."
    create_a_backup_file(entry)
    backup_copy = backups?.last # Refer to the last entry there.
    n_pages = PdfParadise.n_pages?(backup_copy)
    e "#{rev}Now that a backup file has been created, it is time to "\
      "work on that backup"
    e "#{rev}copy. The .pdf currently has #{steelblue(n_pages)} #{rev}pages."
    # ===================================================================== #
    # Always cd into the target directory next - we have to remember
    # the original working directory, though, so that we cd back
    # into it:
    # ===================================================================== #
    if temp_dir and File.directory?(temp_dir)
      @internal_hash[:original_pwd] = temp_dir
      cd(temp_dir)
    end
    new_path = do_delete_the_first_page_of_this_pdf_file(backup_copy)
    n_pages = PdfParadise.n_pages?(new_path)
    e "#{rev}The .pdf file now has #{steelblue(n_pages)} #{rev}pages."
    e 'It can be found at '+sfile(new_path)+'.' if File.exist?(new_path)
    # ===================================================================== #
    # === Keeping track of changes via a log-file
    #
    # Sine as of July 2023 we will also log into a .yml file. We will
    # store the original .pdf file, though, as the modified .pdf
    # file tends to be volatile.
    # ===================================================================== #
    if File.exist?(entry)
      store_this_pdf_file_in_the_log_file(entry) # See the explanation above.
    end
    if @internal_hash[:shall_we_overwrite_the_original_pdf_file] and
       File.exist?(new_path)
      e "#{rev}The variable #{gold(':shall_we_overwrite_the_original_pdf_file')}"\
        " #{rev}was set to"
      e "#{rev}true, so the file at"
      e "#{sfile(entry)}#{rev} will be overwritten now."
      # =================================================================== #
      # In this case we will overwrite the original .pdf file. The
      # following call to mv() does this.
      # =================================================================== #
      delete_file(entry) if File.exist?(new_path)
      mv(
        new_path,
        entry
      )
    end
  }
  cd(@internal_hash[:original_pwd]) # And go back to the initial working directory again.
end

#name_of_the_output_file?(i) ⇒ Boolean Also known as: output?

#

name_of_the_output_file?

This method will determine the output file of the .pdf file.

#

Returns:

  • (Boolean)


327
328
329
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 327

def name_of_the_output_file?(i)
  i.delete_suffix('.pdf')+'_output_file.pdf'
end

#resetObject

#

reset (reset tag)

#


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 90

def reset
  super()
  infer_the_namespace
  reset_the_internal_hash
  # ======================================================================= #
  # === :work_on_these_files
  # ======================================================================= #
  @internal_hash[:work_on_these_files] = []
  # ======================================================================= #
  # === :array_backup_files
  #
  # We also keep track of backup fiels next.
  # ======================================================================= #
  @internal_hash[:array_backup_files] = []
  # ======================================================================= #
  # === :use_this_pdf_application
  #
  # The following variable may have any of the following values:
  #
  #   :hexapdf
  #   :pdftk
  #   :qpdf
  #
  # It specifies which application can be used for deleting .pdf pages.
  # ======================================================================= #
  @internal_hash[:use_this_pdf_application] = :hexapdf # :qpdf # :hexapdf # :pdftk
  # ======================================================================= #
  # === :original_pwd
  #
  # Keep track of the original working directory.
  # ======================================================================= #
  @internal_hash[:original_pwd] = return_pwd
  # ======================================================================= #
  # === :shall_we_overwrite_the_original_pdf_file
  # ======================================================================= #
  @internal_hash[:shall_we_overwrite_the_original_pdf_file] =
    SHALL_WE_OVERWRITE_THE_ORIGINAL_PDF_FILE
end

#runObject

#

run (run tag)

#


153
154
155
156
157
158
159
160
161
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 153

def run
  determine_the_input_files
  _ = work_on_these_files?
  if _.empty?
    opne 'No .pdf file was assigned to this class.'
  else
    do_work_on_these_pdf_files(_)
  end
end

#shall_we_overwrite_the_original_pdf_file?Boolean

#

shall_we_overwrite_the_original_pdf_file?

#

Returns:

  • (Boolean)


297
298
299
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 297

def shall_we_overwrite_the_original_pdf_file?
  @internal_hash[:shall_we_overwrite_the_original_pdf_file]
end

#store_this_pdf_file_in_the_log_file(i) ⇒ Object

#

store_this_pdf_file_in_the_log_file

#


304
305
306
307
308
309
310
311
312
313
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 304

def store_this_pdf_file_in_the_log_file(i)
  PdfParadise.set_use_this_file(i)
  what = YAML.dump(i)
  # ======================================================================= #
  # Find out the full, absolute path to the .yml file next:
  # ======================================================================= #
  into = File.absolute_path("#{log_directory?}last_modified_pdf_file.yml")
  e "#{rev}Logging information into the file `#{sfile(into)}#{rev}`."
  write_what_into(what, into)
end

#use_hexapdf?Boolean

#

use_hexapdf?

#

Returns:

  • (Boolean)


318
319
320
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 318

def use_hexapdf?
  use_this_pdf_application? == :hexapdf
end

#use_pdftk?Boolean

#

use_pdftk?

#

Returns:

  • (Boolean)


139
140
141
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 139

def use_pdftk?
  use_this_pdf_application? == :pdftk
end

#use_this_pdf_application?Boolean

#

use_this_pdf_application?

#

Returns:

  • (Boolean)


132
133
134
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 132

def use_this_pdf_application?
  @internal_hash[:use_this_pdf_application]
end

#work_on_these_files?Boolean Also known as: input?, input_files?

#

work_on_these_files?

#

Returns:

  • (Boolean)


226
227
228
# File 'lib/pdf_paradise/utility_scripts/delete_the_first_page_of_this_pdf_file/delete_the_first_page_of_this_pdf_file.rb', line 226

def work_on_these_files?
  @internal_hash[:work_on_these_files]
end