Class: HexaPDF::CLI::Usage

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

Overview

Shows the space usage of various parts of a PDF file.

Defined Under Namespace

Modules: PDFDataExtension, ParserExtension

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeUsage

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hexapdf/cli/usage.rb', line 100

def initialize #:nodoc:
  super('usage', takes_commands: false)
  short_desc("Show space usage of various parts of a PDF file")
  long_desc(<<~EOF)
    This command displays some usage statistics of the PDF file, i.e. which parts take which
    approximate space in the file.

    Each statistic line shows the space used followed by the number of indirect objects in
    parentheses. If some of those objects are in object streams, that number is displayed
    after a slash.
  EOF

  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? read_password : pwd)
  end

  @password = nil
end

Instance Method Details

#execute(file) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/hexapdf/cli/usage.rb', line 120

def execute(file) #:nodoc:
  HexaPDF::Parser.prepend(ParserExtension)
  HexaPDF::PDFData.prepend(PDFDataExtension)

  with_document(file, password: @password) do |doc|
    # Prepare cache of outline items
    outline_item_cache = {}
    if doc.catalog.key?(:Outlines)
      doc.outline.each_item {|item| outline_item_cache[item] = true }
      outline_item_cache[doc.outline] = true
    end

    doc.revisions.each.with_index do |rev, index|
      sum = count = 0
      categories = {
        Content: [],
        Files: [],
        Fonts: [],
        Images: [],
        Metadata: [],
        ObjectStreams: [],
        Outline: [],
        XObjects: [],
      }
      puts if index > 0
      puts "Usage information for revision #{index + 1}" if doc.revisions.count > 1
      rev.each do |obj|
        if command_parser.verbosity_info?
          print "(#{obj.oid},#{obj.gen}): #{obj.data.size.to_i}"
          print " (#{obj.data.size_in_object_stream})" if obj.data.size.nil?
          puts
        end
        next unless obj.kind_of?(HexaPDF::Dictionary)

        case obj.type
        when :Page
          Array(obj[:Contents]).each do |content|
            categories[:Content] << content if object_in_rev?(content, rev)
          end
        when :Font
          categories[:Fonts] << obj
        when :FontDescriptor
          categories[:Fonts] << obj
          [:FontFile, :FontFile2, :FontFile3].each do |name|
            categories[:Fonts] << obj[name] if object_in_rev?(obj[name], rev)
          end
        when :Metadata
          categories[:Metadata] << obj
        when :Filespec
          categories[:Files] << obj
          categories[:Files] << obj.embedded_file_stream if obj.embedded_file?
        when :ObjStm
          categories[:ObjectStreams] << obj
        else
          if obj[:Subtype] == :Image
            categories[:Images] << obj
          elsif obj[:Subtype] == :Form
            categories[:XObjects] << obj
          end
        end
        sum += obj.data.size if obj.data.size
        count += 1
      end

      # Populate Outline category
      outline_item_cache.reject! do |obj, _val|
        object_in_rev?(obj, rev) && categories[:Outline] << obj
      end

      categories.each do |name, data|
        next if data.empty?
        object_stream_count = 0
        category_sum = data.sum do |o|
          object_stream_count += 1 unless o.data.size
          o.data.size.to_i
        end
        object_stream_count = object_stream_count > 0 ? "/#{object_stream_count}" : ''
        size = human_readable_file_size(category_sum)
        puts "#{name.to_s.ljust(15)} #{size.rjust(8)} (#{data.count}#{object_stream_count})"
      end
      puts "#{'Total'.ljust(15)} #{human_readable_file_size(sum).rjust(8)} (#{count})"
    end
  end
end