Class: HexaPDF::CLI::DebugInfo

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

Overview

Creates debugging information for adding to an issue.

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeDebugInfo

:nodoc:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hexapdf/cli/debug_info.rb', line 45

def initialize #:nodoc:
  super('debug-info', takes_commands: false)
  short_desc("Create debug information for a PDF file")
  long_desc(<<~EOF)
    Creates debug information for a possibly malformed PDF file that can be attached to an
    issue.

    Two files are created: anonymized-FILE where all strings are replaced with zeroes and
    debug_info.txt with additional debug information.
  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:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hexapdf/cli/debug_info.rb', line 64

def execute(file) #:nodoc:
  output_name = "anonymized-#{file}"
  puts "Creating anonymized file '#{output_name}'"
  data = File.binread(file)
  data.gsub!(/(>>\s*stream\s*)(.*?)(\s*endstream)/m) {|m| "#{$1}#{'0' * $2.length}#{$3}" }
  data.gsub!(/([^<]<)([0-9A-Fa-f#{Tokenizer::WHITESPACE}]*?)>/m) {|m| "#{$1}#{'0' * $2.length}>" }
  data.gsub!(/\((.*?)\)/m) {|m| "(#{'0' * $1.length})" }
  File.binwrite(output_name, data)

  debug_info = +''
  puts "Collecting debug information in debug_info.txt"
  begin
    output = capture_output { HexaPDF::CLI::Application.new.parse(['info', '--check', file]) }
    debug_info << "Output:\n"<< output
  rescue
    debug_info << "Error collecting info: #{$!.message}\n"
  end
  File.write('debug_info.txt', debug_info)
end