Sha256: 14743fd67f36d3ba982545eba53701d65aab9a142c6525473ad052eb989f8d3f

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'chrome_remote'

module Postdoc

  class Client
    attr_accessor :client

    def initialize(port)
      @port = port
      100.times { setup_connection_or_wait && break }
      raise 'ChromeClient couldn\'t launch' if @client.blank?
    end

    # We should move away from passing options like this and collect them in
    # the prinbt settings.
    def print_pdf_from_html(file_path,
        settings: PrintSettings.new)

      client.send_cmd 'Page.enable'
      client.send_cmd 'Page.navigate', url: "file://#{file_path}"
      client.wait_for 'Page.loadEventFired'

      response = client.send_cmd 'Page.printToPDF', settings.to_cmd
      
      client.send_cmd 'Browser.close'

      Base64.decode64 response['data']
    end

    def print_document(file_path, settings: PrintSettings.new)
      client.send_cmd 'Page.enable'
      client.send_cmd 'Page.navigate', url: "file://#{file_path}"
      client.wait_for 'Page.loadEventFired'
      response = client.send_cmd 'Page.printToPDF', settings.to_cmd

      client.send_cmd 'Browser.close'

      Base64.decode64 response['data']
    end

    private

    def setup_connection_or_wait
      @client = ChromeRemote.client(port: @port)
      true
    rescue
      sleep(0.1)
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postdoc-0.3.8 lib/postdoc/client.rb