Class: WkHtml::Converter

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

Defined Under Namespace

Classes: Failed

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Converter

Returns a new instance of Converter.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wkhtml/converter.rb', line 15

def initialize(data, options = {})
  @data = data
  #Thanks http://stackoverflow.com/questions/800122/best-way-to-convert-strings-to-symbols-in-hash
  @options = options.inject({}){|memo,(k,v)| memo[k.to_s()] = v; memo} #Force string for easy comparison with allowed settings
  
  #Handle multiple inbound types
  @use_data = case @data
  when String
    !(@data =~ CommonSettings::REGEXP_URI)
  when File, Tempfile
    @data = @data.path
    false
  when URI
    @data = @data.to_s()
    false
  else
    false
  end
end

Instance Method Details

#to_bmpObject



89
# File 'lib/wkhtml/converter.rb', line 89

def to_bmp(); self.to_image(CommonSettings::BMP); end

#to_file(path, format = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wkhtml/converter.rb', line 38

def to_file(path, format = nil)
  path = CommonSettings::cleanup_path(path)

  unless format
    #Use file extension if available
    format = File.extname(path)
    format.sub!(/^\./, '')
    format = nil if format.empty?()
  end
  format ||= CommonSettings::JPG
  format = format.to_s()

  native_converter = if format == CommonSettings::PDF
    create_pdf_converter do |s|
      s.out = path
    end
  else
    create_image_converter do |s|
      s.fmt = format
      s.out = path
    end
  end

  unless native_converter.convert()
    raise Failed.new(native_converter.http_error_code())
  end

  path
end

#to_image(format = CommonSettings::JPG) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wkhtml/converter.rb', line 71

def to_image(format = CommonSettings::JPG)
  native_converter = if format == CommonSettings::PDF
    create_pdf_converter()
  else
    create_image_converter do |s|
      s.fmt = format
    end
  end

  unless native_converter.convert()
    raise Failed.new(native_converter.http_error_code())
  end
  native_converter.get_output()
end

#to_jpgObject



87
# File 'lib/wkhtml/converter.rb', line 87

def to_jpg(); self.to_image(CommonSettings::JPG); end

#to_pdfObject



86
# File 'lib/wkhtml/converter.rb', line 86

def to_pdf(); self.to_image(CommonSettings::PDF); end

#to_pngObject



88
# File 'lib/wkhtml/converter.rb', line 88

def to_png(); self.to_image(CommonSettings::PNG); end

#to_svgObject



90
# File 'lib/wkhtml/converter.rb', line 90

def to_svg(); self.to_image(CommonSettings::SVG); end