Class: HexaPDF::CLI::Inspect

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

Overview

Shows the internal structure of a PDF file.

Instance Method Summary collapse

Constructor Details

#initializeInspect

:nodoc:



42
43
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
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
# File 'lib/hexapdf/cli/inspect.rb', line 42

def initialize #:nodoc:
  super('inspect', takes_commands: false)
  short_desc("Dig into the internal structure of a PDF file")
  long_desc(<<-EOF.gsub!(/^ */, ''))
    Inspects a PDF file for debugging or testing purposes. This command is useful when one
    needs to inspect the internal object structure or a stream of a PDF file. A PDF object is
    always shown in the PDF syntax.

    If no option is given, the main PDF object, the catalog, is shown. Otherwise the various,
    mutually exclusive display options define the shown content. If multiple such options are
    specified only the last is respected.
  EOF

  options.on("-t", "--trailer", "Show the trailer dictionary.") do
    @exec = :trailer
  end
  options.on("-c", "--page-count", "Print the number of pages.") do
    @exec = :page_count
  end
  options.on("--pages [PAGES]", "Show the pages with their object and generation numbers " \
             "and their associated content streams. If the optional argument PAGES is " \
             "specified, only the specified pages are listed.") do |range|
    @exec = :pages
    @param = range || '1-e'
  end
  options.on("-o", "--object OID[,GEN]", "Show the object with the given object and " \
             "generation numbers. The generation number defaults to 0 if not given.") do |str|
    @exec = :object
    @param = str
  end
  options.on("-s", "--stream OID[,GEN]", "Show the filtered stream data (add --raw to get " \
             "the raw stream data) of the object with the given object and generation " \
             "numbers. The generation number defaults to 0 if not given.") do |str|
    @exec = :stream
    @param = str
    @raw = (@raw ? @raw : false)
  end
  options.on("--raw", "Modifies --stream to show the raw stream data instead of the " \
             "filtered one.") do
    @raw = true
  end

  options.separator("")
  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

  @password = nil
  @exec = :catalog
  @param = nil
  @raw = nil
end

Instance Method Details

#execute(file) ⇒ Object

:nodoc:



96
97
98
99
100
101
102
103
# File 'lib/hexapdf/cli/inspect.rb', line 96

def execute(file) #:nodoc:
  HexaPDF::Document.open(file, decryption_opts: {password: @password}) do |doc|
    send("do_#{@exec}", doc)
  end
rescue HexaPDF::Error => e
  $stderr.puts "Error while processing the PDF file: #{e.message}"
  exit(1)
end