class PDFService::PhantomJSRenderer

Constants

RASTERIZE_SCRIPT_PATH

Attributes

logger[R]

Public Class Methods

new(script: RASTERIZE_SCRIPT_PATH, logger: NullLogger.new, binary: 'phantomjs') click to toggle source
# File lib/pdf_service/phantom_js_renderer.rb, line 6
def initialize(script: RASTERIZE_SCRIPT_PATH, logger: NullLogger.new, binary: 'phantomjs')
  @script = script.freeze
  @logger = logger
  @binary = binary.freeze
  Kernel.warn("Warning! PhantomJS is not available in PATH #{@binary}") if `which #{binary}`.strip.empty?
end

Public Instance Methods

rasterize(url, output, format: 'A4') click to toggle source
# File lib/pdf_service/phantom_js_renderer.rb, line 14
def rasterize(url, output, format: 'A4')
  options = [url, output, format]
  phantomjs(@script, url, options)
end

Private Instance Methods

phantomjs(script_path, _, options = []) click to toggle source
# File lib/pdf_service/phantom_js_renderer.rb, line 20
def phantomjs(script_path, _, options = [])
  command = Shellwords.join([@binary, script_path, *options.map(&:to_s)])
  logger.debug('pdf renderer command: ' << command)
  system(command) || raise(::PDFService::Error.new("could not render PDF file. phantomjs exited with exit code = 0"))
end