Class: PdfInfo::Metadata
- Inherits:
-
Object
- Object
- PdfInfo::Metadata
- Defined in:
- lib/pdf_info.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #convert_pts_to_inches(dimension) ⇒ Object private
- #encrypted? ⇒ Boolean
- #file_size ⇒ Object
- #force_utf8_encoding(str) ⇒ Object private
- #init_error ⇒ Object private
-
#initialize(path) ⇒ Metadata
constructor
A new instance of Metadata.
- #page_size ⇒ Object
- #page_size_inches ⇒ Object
- #pages ⇒ Object
- #parse_result ⇒ Object private
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.) end |
#convert_pts_to_inches(dimension) ⇒ Object (private)
64 65 66 |
# File 'lib/pdf_info.rb', line 64 def convert_pts_to_inches(dimension) dimension / 72.0 end |
#encrypted? ⇒ Boolean
37 38 39 |
# File 'lib/pdf_info.rb', line 37 def encrypted? self['Encrypted'] != 'no' end |
#file_size ⇒ Object
58 59 60 |
# File 'lib/pdf_info.rb', line 58 def file_size self['File size'].scan(/\d+/).first.to_i end |
#force_utf8_encoding(str) ⇒ Object (private)
72 73 74 75 76 77 |
# File 'lib/pdf_info.rb', line 72 def force_utf8_encoding(str) return str if str.valid_encoding? reencoded_str = str.encode(Encoding::UTF_16, invalid: :replace, undef: :replace, replace: '') reencoded_str.encode!(Encoding::UTF_8) end |
#init_error ⇒ Object (private)
68 69 70 |
# File 'lib/pdf_info.rb', line 68 def init_error raise PdfInfo::MetadataReadError.new(@exit_status.exitstatus, @stdout.join('\n')) end |
#page_size ⇒ Object
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_inches ⇒ Object
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 |
#pages ⇒ Object
41 42 43 |
# File 'lib/pdf_info.rb', line 41 def pages self['Pages'].to_i end |
#parse_result ⇒ Object (private)
79 80 81 82 83 84 |
# File 'lib/pdf_info.rb', line 79 def parse_result @stdout.each_with_object({}) do |line, out| key, value = line.split(/:\s+/) out[key.strip] = value.try(:strip) end end |