class WkhtmltopdfRunner::Cmd

Attributes

config[R]
file[R]
options[R]
url[R]

Public Class Methods

new(url:, file:, config:, options: {}) click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 9
def initialize(url:, file:, config:, options: {})
  @url = url
  @file = file
  @config = config
  @options = config.options.merge(options)
end

Public Instance Methods

run() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 16
def run
  validate!
  debug_command!

  err, exit_status = Open3.popen3(*command) do |_stdin, _stdout, stderr, wait_thr|
    [stderr.read, wait_thr.value]
  end

  unless exit_status.success?
    raise WkhtmltopdfRunner::Error,
      "Failed to generate PDF. Status: #{exit_status.exitstatus} Error:\n#{err&.strip}"
  end

  true
end

Private Instance Methods

command() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 44
def command
  command = [wkhtmltopdf_path]
  command << '-q' # Output is in stderr so we need to run in quiet mode
  command += formatted_options
  command << url
  command << file_path
end
debug_command!() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 38
def debug_command!
  return unless config.debug

  config.logger.debug("[WkhtmltopdfRunner] Running #{command.join(' ')}")
end
file_path() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 68
def file_path
  if file.respond_to?(:path)
    file.path.to_s
  else
    file.to_s
  end
end
formatted_options() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 52
def formatted_options
  opts = options.each_with_object([]) do |(key, value), list|
    next if value == false

    dashed_key = WkhtmltopdfRunner::Utils.dasherize(key.to_s)

    list << if value == true
      "--#{dashed_key}"
    else
      ["--#{dashed_key}"].concat(Array(value))
    end
  end

  opts.flatten
end
validate!() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 34
def validate!
  WkhtmltopdfRunner::PathValidator.validate!(wkhtmltopdf_path)
end
wkhtmltopdf_path() click to toggle source
# File lib/wkhtmltopdf_runner/cmd.rb, line 76
def wkhtmltopdf_path
  @wkhtmltopdf_path ||= WkhtmltopdfRunner::Path.new(config.binary_path).call
end