Class: PDFService::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Service

Returns a new instance of Service.



3
4
5
6
# File 'lib/pdf_service/service.rb', line 3

def initialize(config)
  @tempfiles = Utils::TempfileService.new(root: config.tmp_path).freeze
  @renderer = PhantomJSRenderer.new(logger: config.logger).freeze
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



9
10
11
# File 'lib/pdf_service/service.rb', line 9

def renderer
  @renderer
end

#tempfilesObject (readonly)

Returns the value of attribute tempfiles.



8
9
10
# File 'lib/pdf_service/service.rb', line 8

def tempfiles
  @tempfiles
end

Instance Method Details

#render(html) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/pdf_service/service.rb', line 11

def render(html)
  tempfiles.use(extension: 'html') { |input_file|
    input_file.write(html)
    render_url("file://#{input_file.expand_path.to_s.strip}")
  }
rescue Error => ex
  raise ex
rescue => ex
  raise Error.new(ex)
end

#render_url(url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdf_service/service.rb', line 22

def render_url(url)
  tempfiles.use(extension: 'pdf') { |output_file|

    renderer.rasterize(url, output_file.expand_path.to_s)

    return output_file.read
  }
rescue Error => ex
  raise ex
rescue => ex
  raise Error.new(ex)
end