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

Instance Method Summary collapse

Methods included from Command::Extensions

#help_banner

Constructor Details

#initializeImages

:nodoc:



45
46
47
48
49
50
51
52
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
# File 'lib/hexapdf/cli/images.rb', line 45

def initialize #:nodoc:
  super('images', takes_commands: false)
  short_desc("List or extract images from a PDF file")
  long_desc(<<-EOF.gsub!(/^ */, ''))
    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:



80
81
82
83
84
85
86
87
88
# File 'lib/hexapdf/cli/images.rb', line 80

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