Class: PdfParadise::DeleteThisPageOfThisPdfFile

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

Constant Summary collapse

OVERWRITE_THE_OLD_PDF_FILE =
#

OVERWRITE_THE_OLD_PDF_FILE

If the following constant is set to true then the given input file will be overwritten. I prefer this on my home system, so the default is true for now.

#
true
DEFAULT_REMOVE_THIS_PDF_PAGE =
#

DEFAULT_REMOVE_THIS_PDF_PAGE

If you don’t feel like manually passing data to the .rb file, use the hardcoded value here.

#
126

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, #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(input_files = ARGV, run_already = true) ⇒ DeleteThisPageOfThisPdfFile

#

initialize

#


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 49

def initialize(
    input_files = ARGV,
    run_already = true
  )
  reset
  set_commandline_arguments(
    input_files
  )
  set_input_files(
    input_files
  )
  # ======================================================================= #
  # === Handle Blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :do_not_run_yet
      run_already = false
    end
  end
  run if run_already
end

Instance Method Details

#do_not_overwrite_the_original_pdf_fileObject

#

do_not_overwrite_the_original_pdf_file

#


166
167
168
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 166

def do_not_overwrite_the_original_pdf_file
  @overwrite_the_old_pdf_file = false
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


184
185
186
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 184

def input?
  @input_files
end
#

menu (menu tag)

#


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
293
294
295
296
297
298
299
300
301
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 268

def menu(
    i = return_commandline_arguments_starting_with_hyphens
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === deletethispageofthispdffile DONE*pdf --remove-this-page-number=430
    #
    # Remove specific pages from a given .pdf file.
    #
    # Usage example:
    #
    #   pagegone DONE*pdf --remove-this-page-number=360
    #   pagegone DONE*pdf --remove=94
    #   pagegone DONE*pdf --remove=162
    #   pagegone DONE*pdf --remove=307
    #
    # ===================================================================== #
    when /^-?-?remove(-|_)?this(-|_)?page(-|_)?number=(.+)$/i, # === $4
         /^-?-?remove=(.+)$/i # === $1
      _ = $1.to_s.dup
      _ = $4.to_s.dup if $4
      set_remove_this_pdf_page(_)
    # ===================================================================== #
    # === --help
    # ===================================================================== #
    when /help/
      show_help
      exit
    end
  end
end

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

#

name_of_the_output_file?

#

Returns:

  • (Boolean)


173
174
175
176
177
178
179
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 173

def name_of_the_output_file?(i)
  if overwrite_the_old_pdf_file?
    i.sub(/\.pdf$/,'')+'.pdf'
  else
    i.sub(/\.pdf$/,'')+'_output_file.pdf'
  end
end

#overwrite_the_old_pdf_file?Boolean

#

overwrite_the_old_pdf_file?

#

Returns:

  • (Boolean)


159
160
161
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 159

def overwrite_the_old_pdf_file?
  @overwrite_the_old_pdf_file
end

#process_each_pdfObject

#

process_each_pdf

#


191
192
193
194
195
196
197
198
199
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
231
232
233
234
235
236
237
238
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 191

def process_each_pdf
  @input_files.each {|this_pdf_file|
    # ===================================================================== #
    # The command should be something like this:
    #
    #   qpdf input.pdf --pages input.pdf 1-9,26-z -- outputfile.pdf 
    #
    # See: https://superuser.com/a/1297395
    # ===================================================================== #
    if File.exist? this_pdf_file
      opnn; e 'Now working on the .pdf file '
      e
      e sfancy(this_pdf_file)
      e
      opnn; e ', containing '+n_pages?(this_pdf_file).to_s+' pages.'
      opnn; e 'The page that will be removed is: '+
              steelblue(@remove_this_pdf_page.to_s)
      case @use_this_program
      when /qpdf/
        _ = @use_this_program.dup
        _ << " #{this_pdf_file}"
        _ << " --pages #{this_pdf_file} "+return_the_proper_range
        _ << " -- output.pdf"
      when /hexapdf/
        _ = @use_this_program.dup
        _ << ' modify'
        _ << " #{this_pdf_file}"
        _ << ' -i '+return_the_proper_range.tr('z','e')
        _ << " output.pdf"
      end
      e
      esystem _
      e
      if overwrite_the_old_pdf_file?
        output_file = 'output.pdf'
        if File.exist? output_file
          File.delete(this_pdf_file) if File.exist? this_pdf_file
          move_file(output_file, this_pdf_file)
        end
      end
      opnn; e 'The .pdf file at '+sfancy(this_pdf_file)+
              rev+' now contains '+n_pages?(this_pdf_file).to_s+rev+
              ' pages.'
    else
      opnn; e "{rev}#No file exists at `#{sfile(this_pdf_file)}#{rev}`."
    end
  }
end

#remove_this_pdf_page?Boolean Also known as: page_number?

#

remove_this_pdf_page?

#

Returns:

  • (Boolean)


243
244
245
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 243

def remove_this_pdf_page?
  @remove_this_pdf_page
end

#resetObject

#

reset

#


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 76

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @overwrite_the_old_pdf_file
  #
  # This determines whether the old .pdf file will be overwritten.
  # ======================================================================= #
  @overwrite_the_old_pdf_file = OVERWRITE_THE_OLD_PDF_FILE
  # ======================================================================= #
  # === @remove_this_pdf_page
  #
  # This variable keeps track as to which particular page we wish
  # to remove from our .pdf file. By default we will set to one
  # as is anyway.
  # ======================================================================= #
  @remove_this_pdf_page = DEFAULT_REMOVE_THIS_PDF_PAGE
  # ======================================================================= #
  # === @use_this_program
  # ======================================================================= #
  @use_this_program = 'hexapdf' # qpdf'
end

#return_the_proper_rangeObject

#

return_the_proper_range

This method will return the proper range, such as:

1-9,11-z

The ‘,’ will be included in the return value by this method.

#


150
151
152
153
154
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 150

def return_the_proper_range
  before_part = "1-#{(@remove_this_pdf_page - 1).to_s}"
  after_part  = (@remove_this_pdf_page + 1).to_s+'-z'
  "#{before_part},#{after_part}"
end

#runObject

#

run

#


306
307
308
309
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 306

def run
  menu
  process_each_pdf
end

#sanitize_input_filesObject

#

sanitize_input_files

#


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 122

def sanitize_input_files
  unless @input_files.empty?
    set_page_number(
      @input_files.select {|entry|
        # ================================================================= #
        # Get numbers.
        # ================================================================= #
        entry =~ /^\d+$/
      }
    )
    # ===================================================================== #
    # Select only .pdf files next.
    # ===================================================================== #
    @input_files.select! {|entry|
      entry.end_with? '.pdf'
    }
  end
end

#set_input_files(i) ⇒ Object Also known as: set_pdf_file, set_pdf_files

#

set_input_files

#


112
113
114
115
116
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 112

def set_input_files(i)
  i = [i] unless i.is_a? Array
  @input_files = i
  sanitize_input_files
end

#set_remove_this_pdf_page(i = nil) ⇒ Object Also known as: set_page_number

#

set_remove_this_pdf_page

#


250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 250

def set_remove_this_pdf_page(
    i = nil
  )
  if i
    if i.is_a? Array
      i = i.first
    end
    if i.nil?
      # In this case use the default value.
      i = DEFAULT_REMOVE_THIS_PDF_PAGE
    end
    @remove_this_pdf_page = i.to_i
  end
end

#show_helpObject

#

show_help (help tag)

#


102
103
104
105
106
107
# File 'lib/pdf_paradise/utility_scripts/delete_this_page_of_this_pdf_file.rb', line 102

def show_help
  e
  e 'The first argument should be the .pdf file.'
  e 'The second argument should be the page number.'
  e
end