Class: PdfParadise::ExtractPdfPage

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

Overview

PdfParadise::ExtractPdfPage

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
DEFAULT_START_PAGE =
#

DEFAULT_START_PAGE

#
'1'
DEFAULT_END_PAGE =
#

DEFAULT_END_PAGE

This number can be modified automatically during runtime.

#
'10'
DEFAULT_EXTRACT_N_PDF_FILES =
#

DEFAULT_EXTRACT_N_PDF_FILES

#
1

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(mandatory_name_of_pdf_file = nil, optional_start_page = DEFAULT_START_PAGE, extract_n_pdf_files = DEFAULT_EXTRACT_N_PDF_FILES, run_already = true) ⇒ ExtractPdfPage

#

initialize

We accept three arguments:

(1) the name of the .pdf file
(2) the start page
(3) the end page
#


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 73

def initialize(
    mandatory_name_of_pdf_file = nil,
    optional_start_page = DEFAULT_START_PAGE, 
    extract_n_pdf_files = DEFAULT_EXTRACT_N_PDF_FILES,
    run_already         = true
  )
  reset
  check_against_menu(mandatory_name_of_pdf_file)
  if mandatory_name_of_pdf_file.is_a? Hash
    if mandatory_name_of_pdf_file.has_key? :dont_run_yet 
      run_already = !mandatory_name_of_pdf_file.delete(:dont_run_yet)
    end
  end
  set_name_of_pdf_file(mandatory_name_of_pdf_file) # Must come before set_extract_n_pdf_files()
  unless mandatory_name_of_pdf_file.is_a? Hash
    set_start_page(optional_start_page)
    set_extract_n_pdf_files(extract_n_pdf_files)
  end
  run if run_already
end

Instance Method Details

#calculate_last_pageObject

#

calculate_last_page

This will calculate which page must be the last.

#


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

def calculate_last_page
  return start_page?.to_i + (@extract_n_pdf_files.to_i - 1)
end

#check_against_menu(i = nil) ⇒ Object

#

check_against_menu (menu tag)

#


162
163
164
165
166
167
168
169
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 162

def check_against_menu(i = nil)
  case i
  when '--help','HELP'
    e 'Usage example:'
    e '  ext_pdf foo.pdf 1 100'
    exit
  end
end

#determine_output_fileObject

#

determine_output_file

#


246
247
248
249
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 246

def determine_output_file
  # @output_file = 'extracted_page_'+which_page?+'_from_file_'+name_of_pdf_file?
  @output_file = 'extracted_page_from_file_'+name_of_pdf_file?
end

#difference?Boolean Also known as: n_times

#

difference?

#

Returns:

  • (Boolean)


190
191
192
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 190

def difference?
  return (calculate_last_page - start_page?.to_i) 
end

#name_of_pdf_file?Boolean Also known as: input_file?, name_of_the_pdf_file?

#

name_of_pdf_file?

#

Returns:

  • (Boolean)


222
223
224
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 222

def name_of_pdf_file?
  @name_of_pdf_file
end

#opnnObject

#

opnn

#


266
267
268
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 266

def opnn
  super(NAMESPACE)
end

#output_file?Boolean

#

output_file?

#

Returns:

  • (Boolean)


197
198
199
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 197

def output_file?
  @output_file
end

#report_to_the_userObject

#

report_to_the_user

#


214
215
216
217
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 214

def report_to_the_user
  report_to_the_user_how_many_pages_we_extracted
  report_to_the_user_where_we_stored_the_new_pdf_file
end

#report_to_the_user_how_many_pages_we_extractedObject

#

report_to_the_user_how_many_pages_we_extracted

#


204
205
206
207
208
209
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 204

def report_to_the_user_how_many_pages_we_extracted
  n_pdf_pages = @extract_n_pdf_files.to_i
  if File.exist? input_file?
    opnn; e 'We extracted '+sfancy(n_pdf_pages.to_s)+' pdf pages.'
  end
end

#report_to_the_user_where_we_stored_the_new_pdf_fileObject

#

report_to_the_user_where_we_stored_the_new_pdf_file

#


152
153
154
155
156
157
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 152

def report_to_the_user_where_we_stored_the_new_pdf_file
  _ = output_file?
  if File.exist? _
    opnn; e 'Finished storing at `'+sfile(_)+'`.'
  end
end

#resetObject

#

reset (reset tag)

#


97
98
99
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 97

def reset
  super()
end

#runObject

#

run (run tag)

#


273
274
275
276
277
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 273

def run
  determine_output_file
  run_verbose_system_command
  report_to_the_user
end

#run_verbose_system_commandObject

#

run_verbose_system_command

#


230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 230

def run_verbose_system_command
  n_times.times.each {|index|
    start = start_page?.to_i+index.to_i
    _ = ''.dup
    _ << 'gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER'
    _ << ' -dFirstPage='+start.to_s
    _ << ' -dLastPage='+( start.to_i ).to_s
    _ << ' -sOutputFile='+start.to_s+'_'+output_file?
    _ << ' '+name_of_pdf_file?.to_s
    esystem _
  }
end

#set_extract_n_pdf_files(i = DEFAULT_EXTRACT_N_PDF_FILES) ⇒ Object Also known as: set_end_page

#

set_end_page

#


254
255
256
257
258
259
260
261
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 254

def set_extract_n_pdf_files(i = DEFAULT_EXTRACT_N_PDF_FILES)
  i = DEFAULT_EXTRACT_N_PDF_FILES if i.nil?
  i = i.to_i
  if i > ::PdfParadise.n_pdf_pages?(name_of_the_pdf_file?)
    i = ::PdfParadise.n_pdf_pages?(name_of_the_pdf_file?)
  end
  @extract_n_pdf_files = i
end

#set_name_of_pdf_file(i) ⇒ Object Also known as: use_this_input_file

#

set_name_of_pdf_file

The input can also be a Hash.

#


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 106

def set_name_of_pdf_file(i)
  if i.is_a? Hash
    if i.has_key? :start_page
      set_start_page(i.delete(:start_page))
    end
    if i.has_key? :end_page
      set_end_page(i.delete(:end_page))
    end
    if i.has_key? :pdf_file_to_use
      i = i.delete(:pdf_file_to_use)
    end
  else
    i = i.to_s
  end
  unless File.exist? i
    opnn; e 'Warning - no file at `'+sfile(i.to_s)+'` could be found.'
    opnn; e 'Thus we can not extract anything. Exiting now'
    exit
  end
  @name_of_pdf_file = i
end

#set_start_page(i = DEFAULT_START_PAGE) ⇒ Object

#

set_start_page

#


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

def set_start_page(
    i = DEFAULT_START_PAGE
  )
  i = DEFAULT_START_PAGE if i.nil?
  if i.is_a? Hash
    if i.has_key? :save_here
      set_save_here(i.delete(:save_here))
    end
  end
  i = i.to_s
  if File.exist?(i) and i.include? '.pdf'
    use_this_input_file(i)
    i = DEFAULT_START_PAGE
  end
  i = i.to_i
  @start_page = i
end

#start_page?Boolean Also known as: which_page?

#

start_page?

#

Returns:

  • (Boolean)


174
175
176
# File 'lib/pdf_paradise/utility_scripts/extract_pdf_page.rb', line 174

def start_page?
  @start_page.to_s
end