Class: Gkhtmltopdf::Converter

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

Constant Summary collapse

DEFAULT_FX_USER_AGENT =
"gkhtmltopdf-rb(v#{VERSION}) by firefox and gecko".freeze

Instance Method Summary collapse

Instance Method Details

#closeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gkhtmltopdf/converter.rb', line 29

def close
  delete_session! if @session_id
  delete_tmp_profile! if @profile_path
  begin
    unless @pid.nil?
      Process.kill('TERM', @pid)
      Process.wait(@pid)
    end
  rescue Errno::ESRCH, Errno::ECHILD
    # nothing to do if the process is already terminated
  end
  nil
end

#open(geckodriver_path: nil, firefox_path: nil, wait_time: nil, port: nil, user_agent: nil, gecko_stdout: nil, gecko_stderr: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gkhtmltopdf/converter.rb', line 15

def open(geckodriver_path: nil, firefox_path: nil, wait_time: nil, port: nil, user_agent: nil, gecko_stdout: nil, gecko_stderr: nil)
  @geckodriver_path = resolve_geckodriver_path!(geckodriver_path)
  @firefox_path = resolve_firefox_path!(firefox_path)
  @port = port || get_free_port
  @base_url = "http://127.0.0.1:#{@port}"
  gecko_stdout = File::NULL if gecko_stdout.nil?
  gecko_stderr = File::NULL if gecko_stderr.nil?
  @pid = spawn("#{@geckodriver_path} --port #{@port}", out: gecko_stdout, err: gecko_stderr)
  wait_time ||= 20
  @profile_path = gen_tmp_profile(user_agent)
  wait_for_gk(wait_time)
  create_session!
end

#save_pdf(url, output_path, print_options: {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/gkhtmltopdf/converter.rb', line 43

def save_pdf(url, output_path, print_options: {})
  validate_url_scheme!(url)
  navigate(url)
  pdf_base64 = print_pdf(print_options)
  File.binwrite(output_path, Base64.decode64(pdf_base64))
end