Class: PdfParadise::DeleteLastPageOfThisPdfFile

Inherits:
Base
  • Object
show all
Defined in:
lib/pdf_paradise/utility_scripts/delete_last_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.

#
true

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) ⇒ DeleteLastPageOfThisPdfFile

#

initialize

#


37
38
39
40
41
42
43
44
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 37

def initialize(
    input_files = ARGV,
    run_already = true
  )
  reset
  set_input_files(input_files)
  run if run_already
end

Instance Method Details

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


153
154
155
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 153

def input?
  @input_files
end

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

#

name_of_the_output_file?

#

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 135

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

#process_each_pdfObject

#

process_each_pdf

#


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
128
129
130
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 93

def process_each_pdf
  @input_files.each {|this_pdf_file|
    # ===================================================================== #
    # First, we must find out how many pdf pages are in the given
    # pdf file at hand.
    # ===================================================================== #
    if File.exist? this_pdf_file
      has_n_pages = ::PdfParadise.has_n_pages?(this_pdf_file)
      opne 'Now working on the .pdf file '+sfancy(this_pdf_file)+
           rev+' ('+steelblue(has_n_pages.to_s+' pages')+rev+')'
      has_n_pages = PdfParadise.n_pages_in_this_pdf_file?(this_pdf_file) # This is also the last page.
      if use_pdftk?
        _ = 'pdftk '+this_pdf_file+' cat 1-'+(has_n_pages.to_i - 1).to_s+' '+
             has_n_pages.to_s+'-end output '+
             name_of_the_output_file?(this_pdf_file)
      else # else we use qpdf
        _ = 'qpdf'.dup
        if @overwrite_the_old_pdf_file
          _ << ' --replace-input'
        end
        if @overwrite_the_old_pdf_file
          _ << ' --pages '+this_pdf_file+' 1-'+(has_n_pages.to_i - 1).to_s+' -- '+
               this_pdf_file
        else
          _ << ' --pages '+this_pdf_file+' 1-'+(has_n_pages.to_i - 1).to_s+' -- '+
               this_pdf_file+' '+name_of_the_output_file?(this_pdf_file)
        end
      end
      begin
        esystem _ # Need to rescue here, e. g. due to permission errors and what not.
      rescue Exception => error
        pp error
      end
    else
      opne 'No file exists at `'+sfile(this_pdf_file)+'`.'
    end
  }
end

#resetObject

#

reset

#


49
50
51
52
53
54
55
56
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 49

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @overwrite_the_old_pdf_file
  # ======================================================================= #
  @overwrite_the_old_pdf_file = OVERWRITE_THE_OLD_PDF_FILE
end

#runObject

#

run

#


160
161
162
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 160

def run
  process_each_pdf
end

#sanitize_input_filesObject

#

sanitize_input_files

#


79
80
81
82
83
84
85
86
87
88
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 79

def sanitize_input_files
  unless @input_files.empty?
    # ===================================================================== #
    # Select only .pdf files next.
    # ===================================================================== #
    @input_files.select! {|entry|
      entry.end_with? '.pdf'
    }
  end
end

#set_input_files(i) ⇒ Object

#

set_input_files

#


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pdf_paradise/utility_scripts/delete_last_page_of_this_pdf_file.rb', line 61

def set_input_files(i)
  i = [i] unless i.is_a? Array
  if i.empty? and is_on_roebe?
    # ===================================================================== #
    # In this case we will use the main-pdf file, if it exists.
    # ===================================================================== #
    if ENV.has_key?('MAIN_BOOK') and
      File.exist?(ENV['MAIN_BOOK'])
      i << ENV['MAIN_BOOK'].dup
    end
  end
  @input_files = i
  sanitize_input_files
end

#use_pdftk?Boolean

#

use_pdftk?

#

Returns:

  • (Boolean)


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

def use_pdftk?
  false
end