Class: WickedPdf::Binary

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

Constant Summary collapse

EXE_NAME =
'wkhtmltopdf'.freeze
MINIMUM_BINARY_VERSION =
Gem::Version.new('0.12.0')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary_path = nil) ⇒ Binary

Returns a new instance of Binary.



23
24
25
26
27
28
29
30
31
# File 'lib/wicked_pdf/binary.rb', line 23

def initialize(binary_path = nil)
  @path = binary_path || find_binary_path
  @version = retrieve_binary_version

  raise "Minimum version of wkhtmltopdf is #{MINIMUM_BINARY_VERSION}. Version found was #{@version}." if @version < MINIMUM_BINARY_VERSION
  raise "Location of #{EXE_NAME} unknown" if @path.empty?
  raise "Bad #{EXE_NAME}'s path: #{@path}" unless File.exist?(@path)
  raise "#{EXE_NAME} is not executable" unless File.executable?(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/wicked_pdf/binary.rb', line 6

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/wicked_pdf/binary.rb', line 6

def version
  @version
end

Class Method Details

.defaultObject

Shared instance, so the version check (wkhtmltopdf -V subprocess) runs once instead of once per render. Re-resolved whenever config.exe_path changes — including back to nil (automatic path detection).



11
12
13
14
15
16
# File 'lib/wicked_pdf/binary.rb', line 11

def self.default
  configured_path = WickedPdf.config.exe_path
  @default = nil if @default && @default_configured_path != configured_path
  @default_configured_path = configured_path
  @default ||= new
end

.reset_default!Object



18
19
20
21
# File 'lib/wicked_pdf/binary.rb', line 18

def self.reset_default!
  @default = nil
  @default_configured_path = nil
end

Instance Method Details

#parse_version_string(version_info) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/wicked_pdf/binary.rb', line 33

def parse_version_string(version_info)
  match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
  if match_data && (match_data.length == 2)
    Gem::Version.new(match_data[1])
  else
    MINIMUM_BINARY_VERSION
  end
end