Class: HexaPDF::CLI::Extract

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/hexapdf/cli/extract.rb

Overview

Extracts files from a PDF file.

See: HexaPDF::Type::EmbeddedFile

Instance Method Summary collapse

Constructor Details

#initializeExtract

:nodoc:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hexapdf/cli/extract.rb', line 44

def initialize #:nodoc:
  super('extract', takes_commands: false)
  short_desc("Extract files from a PDF file")
  long_desc(<<-EOF.gsub!(/^ */, ''))
    This command extracts files embedded in a PDF file. If the option --indices is not given,
    the available files are listed with their names and indices. The --indices option can then
    be used to extract one or more files.
  EOF
  options.on("--indices a,b,c", "-i a,b,c,...", Array,
             "The indices of the files that should be extracted. Use 0 to extract " \
             "all files.") do |indices|
    @indices = indices.map(&:to_i)
  end
  options.on("--[no-]search", "-s", "Search the whole PDF instead of the " \
             "standard locations (default: false)") do |search|
    @search = search
  end
  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? command_parser.read_password : pwd)
  end
  @indices = []
  @password = ''
  @search = false
end

Instance Method Details

#execute(file) ⇒ Object

:nodoc:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hexapdf/cli/extract.rb', line 70

def execute(file) #:nodoc:
  HexaPDF::Document.open(file, decryption_opts: {password: @password}) do |doc|
    if @indices.empty?
      list_files(doc)
    else
      extract_files(doc)
    end
  end
rescue HexaPDF::Error => e
  $stderr.puts "Error while processing the PDF file: #{e.message}"
  exit(1)
end