Class: PdfParadise::CombineThesePdfPages

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

Overview

PdfParadise::CombineThesePdfPages

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect

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(commandline_arguments = ARGV, run_already = true) ⇒ CombineThesePdfPages

#

initialize

#


25
26
27
28
29
30
31
32
33
34
# File 'lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb', line 25

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Instance Method Details

#opnnObject

#

opnn

#


46
47
48
# File 'lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb', line 46

def opnn
  super(NAMESPACE)
end

#resetObject

#

reset (reset tag)

#


39
40
41
# File 'lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb', line 39

def reset
  super()
end

#runObject

#

run (run tag)

#


53
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb', line 53

def run
  name_of_the_pdf_file = File.absolute_path(first_argument?)
  unless File.exist? name_of_the_pdf_file
    e 'Please provide a path to an existing .pdf file.'
    return
  end
  _ = log_dir?
  mkdir(_) unless File.directory? _
  cd _
  if File.exist? name_of_the_pdf_file
    unless File.exist? File.absolute_path(File.basename(name_of_the_pdf_file))
      e 'Copying the file '+name_of_the_pdf_file+'.'
      copy_file(name_of_the_pdf_file)
    end
    if File.exist? File.basename(name_of_the_pdf_file)
      name_of_the_pdf_file = File.absolute_path(File.basename(name_of_the_pdf_file))
    end
  end
  these_pages_must_be_kept = commandline_arguments?[1]
  # Must be converted into an array.
  if these_pages_must_be_kept.include? ','
    these_pages_must_be_kept = these_pages_must_be_kept.split(',')
  else
    these_pages_must_be_kept = [these_pages_must_be_kept]
  end
  these_pages_must_be_kept.map! {|entry| entry.to_i }
  begin
    require 'hexapdf'
    pdf = HexaPDF::Document.open(name_of_the_pdf_file)
    new_pdf = HexaPDF::Document.new
    pdf.pages.each_with_index { |page, index| index += 1
      if these_pages_must_be_kept.include? index
        e 'Copying page page number: '+
           steelblue(index.to_s)
        new_pdf.pages << new_pdf.import(page)
      end
    }
    @output_file_to_use = 'output.pdf'
    @output_file_to_use = File.absolute_path(@output_file_to_use)
    delete_file(@output_file_to_use) if File.exist? @output_file_to_use
    e 'Storing into `'+@output_file_to_use+'`.'
    new_pdf.write(
      @output_file_to_use,
      validate: false,
      optimize: true
    )
  rescue LoadError
    e 'HexaPDF is not available. Please install it via:'
    e
    e '  gem install hexapdf'
    e
  end
end

#stored_where?Boolean Also known as: output_file_to_use?

#

stored_where?

#

Returns:

  • (Boolean)


110
111
112
# File 'lib/pdf_paradise/utility_scripts/combine_these_pdf_pages.rb', line 110

def stored_where?
  @output_file_to_use
end