Class: PDF::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/info.rb,
lib/pdf/info/version.rb,
lib/pdf/info/exceptions.rb

Defined Under Namespace

Classes: BadPermissionsError, Error, FileError, OutputError, UnexpectedExitError, UnknownError

Constant Summary collapse

VERSION =
"0.5.3"
@@command_path =
"pdfinfo"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf_path) ⇒ Info

Returns a new instance of Info.



16
17
18
# File 'lib/pdf/info.rb', line 16

def initialize(pdf_path)
  @pdf_path = pdf_path
end

Class Method Details

.command_pathObject



12
13
14
# File 'lib/pdf/info.rb', line 12

def self.command_path
  @@command_path
end

.command_path=(path) ⇒ Object



8
9
10
# File 'lib/pdf/info.rb', line 8

def self.command_path=(path)
  @@command_path = path
end

Instance Method Details

#commandObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdf/info.rb', line 20

def command
  output = `#{self.class.command_path} -enc UTF-8 -f 1 -l -1 "#{@pdf_path}" 2> /dev/null`
  exit_code = $?
  case exit_code
  when 0 || nil
    if !output.valid_encoding?
      # It's already UTF-8, so we need to convert to UTF-16 and back to
      # force the bad characters to be replaced.
      output.encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "")
      output.encode!('UTF-8')
    end
    return output
  else
    exit_error = PDF::Info::UnexpectedExitError.new
    exit_error.exit_code = exit_code
    raise exit_error
  end
end

#metadataObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pdf/info.rb', line 39

def 
  begin
    process_output(command)
  rescue UnexpectedExitError => e
    case e.exit_code
    when 1
      raise FileError
    when 2
      raise OutputError
    when 3
      raise BadPermissionsError
    else
      raise UnknownError
    end
  end
end

#process_output(output) ⇒ Object



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
# File 'lib/pdf/info.rb', line 56

def process_output(output)
  rows = output.split("\n")
   = {}
  rows.each do |row|
    pair = row.split(':', 2)
    pair.map!(&:strip)

    case pair.first
    when "Pages"
      [:page_count] = pair.last.to_i
    when "Encrypted"
      [:encrypted] = pair.last == 'yes'
    when "Optimized"
      [:optimized] = pair.last == 'yes'
    when "Tagged"
      [:tagged] = pair.last == 'yes'
    when "PDF version"
      [:version] = pair.last.to_f
    when "CreationDate"
      creation_date = parse_datetime(pair.last)
      [:creation_date] = creation_date if creation_date
    when "ModDate"
      modification_date = parse_datetime(pair.last)
      [:modification_date] = modification_date if modification_date
    when /^Page.*size$/
      [:pages] ||= []
      [:pages] << pair.last.scan(/[\d.]+/).map(&:to_f)
      [:format] = pair.last.scan(/.*\(\w+\)$/).to_s
    when String
      [pair.first.downcase.tr(" ", "_").to_sym] = pair.last.to_s.strip
    end
  end

  
end