class PDFService::Service

Attributes

renderer[R]
tempfiles[R]

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

render(html) click to toggle source
# 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) click to toggle source
# 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