Class: HexaPDF::CLI::Images

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

Overview

Lists or extracts images from a PDF file.

See: HexaPDF::Type::Image

Defined Under Namespace

Classes: ImageLocationProcessor

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeImages

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hexapdf/cli/images.rb', line 83

def initialize #:nodoc:
  super('images', takes_commands: false)
  short_desc("List or extract images from a PDF file")
  long_desc(<<~EOF)
    If the option --extract is not given, the available images are listed with their index and
    additional information, sorted by page number. The --extract option can then be used to
    extract one or more images, saving them to files called `prefix-n.ext` where the prefix
    can be set via --prefix, n is the index and ext is either png, jpg or jpx.
  EOF

  options.on("--extract [A,B,C,...]", "-e [A,B,C,...]", Array,
             "The indices of the images that should be extracted. Use 0 or no argument to " \
               "extract all images.") do |indices|
    @indices = (indices ? indices.map(&:to_i) : [0])
  end
  options.on("--prefix PREFIX", String,
             "The prefix to use when saving images. May include directories. Default: " \
               "image.") do |prefix|
    @prefix = prefix
  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 == '-' ? read_password : pwd)
  end

  @indices = []
  @prefix = 'image'
  @password = nil
  @search = false
end

Instance Method Details

#execute(pdf) ⇒ Object

:nodoc:



118
119
120
121
122
123
124
125
126
# File 'lib/hexapdf/cli/images.rb', line 118

def execute(pdf) #:nodoc:
  with_document(pdf, password: @password) do |doc|
    if @indices.empty?
      list_images(doc)
    else
      extract_images(doc)
    end
  end
end