Class: PdfInfo::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_info.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Metadata

Returns a new instance of Metadata.



19
20
21
22
23
24
25
26
27
28
# File 'lib/pdf_info.rb', line 19

def initialize(path)
  @stdout = []
  Open3.popen2e(Settings.binaries.pdfinfo, path) do |_stdin, stdout, wait|
    stdout.each_line do |line|
      @stdout.push(force_utf8_encoding(line))
    end
    @exit_status = wait.value
  end
  init_error unless @exit_status.success?
end

Class Method Details

.read(file_or_path) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/pdf_info.rb', line 11

def self.read(file_or_path)
  if file_or_path.is_a? String
    new(file_or_path)
  else
    new(file_or_path.path)
  end
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
# File 'lib/pdf_info.rb', line 30

def [](key)
  @internal_hash ||= parse_result
  @internal_hash[key]
rescue => e
  raise PdfInfo::MetadataReadError.new(-1, e.message)
end

#encrypted?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/pdf_info.rb', line 37

def encrypted?
  self['Encrypted'] != 'no'
end

#file_sizeObject



58
59
60
# File 'lib/pdf_info.rb', line 58

def file_size
  self['File size'].scan(/\d+/).first.to_i
end

#page_sizeObject



45
46
47
48
# File 'lib/pdf_info.rb', line 45

def page_size
  width, height = self['Page size'].scan(/\d+/).map(&:to_i)
  { width:, height: }
end

#page_size_inchesObject



50
51
52
53
54
55
56
# File 'lib/pdf_info.rb', line 50

def page_size_inches
  in_pts = page_size
  {
    height: convert_pts_to_inches(in_pts[:height]),
    width: convert_pts_to_inches(in_pts[:width])
  }
end

#pagesObject



41
42
43
# File 'lib/pdf_info.rb', line 41

def pages
  self['Pages'].to_i
end