Class: PDF::Info
- Inherits:
-
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_path ⇒ Object
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
#command ⇒ Object
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?
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
|
#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")
metadata = {}
rows.each do |row|
pair = row.split(':', 2)
pair.map!(&:strip)
case pair.first
when "Pages"
metadata[:page_count] = pair.last.to_i
when "Encrypted"
metadata[:encrypted] = pair.last == 'yes'
when "Optimized"
metadata[:optimized] = pair.last == 'yes'
when "Tagged"
metadata[:tagged] = pair.last == 'yes'
when "PDF version"
metadata[:version] = pair.last.to_f
when "CreationDate"
creation_date = parse_datetime(pair.last)
metadata[:creation_date] = creation_date if creation_date
when "ModDate"
modification_date = parse_datetime(pair.last)
metadata[:modification_date] = modification_date if modification_date
when /^Page.*size$/
metadata[:pages] ||= []
metadata[:pages] << pair.last.scan(/[\d.]+/).map(&:to_f)
metadata[:format] = pair.last.scan(/.*\(\w+\)$/).to_s
when String
metadata[pair.first.downcase.tr(" ", "_").to_sym] = pair.last.to_s.strip
end
end
metadata
end
|