Class: Palapala::Renderer
- Inherits:
-
Object
- Object
- Palapala::Renderer
- Defined in:
- lib/palapala/renderer.rb
Overview
Render HTML content to PDF using Chrome in headless mode with minimal dependencies
Defined Under Namespace
Modules: ChromeProcess
Instance Method Summary collapse
- #close ⇒ Object
-
#current_id ⇒ Object
Get the current ID.
-
#html_to_pdf(html, params: {}) ⇒ Object
Convert HTML content to PDF See chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF.
-
#initialize ⇒ Renderer
constructor
A new instance of Renderer.
-
#next_id ⇒ Object
Update the current ID to the next ID (increment by 1).
-
#on_message(e) ⇒ Object
Callback to handle the incomming WebSocket messages.
-
#process_until(&block) ⇒ Object
Process the WebSocket messages until some state is true.
-
#send_and_wait(message) ⇒ Object
Method to send a message (text) and wait for a response.
-
#send_command(method, params: {}, &block) ⇒ Object
Method to send a CDP command and wait for some state to be true.
-
#send_command_and_wait_for_event(method, event_name:, params: {}) ⇒ Object
Method to send a CDP command and wait for a specific method to be called.
-
#send_command_and_wait_for_result(method, params: {}) ⇒ Hash
Method to send a CDP command and wait for the matching event to get the result.
Constructor Details
#initialize ⇒ Renderer
Returns a new instance of Renderer.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/palapala/renderer.rb', line 10 def initialize # Create an instance of WebSocketClient with the WebSocket URL @client = Palapala::WebSocketClient.new(websocket_url) # Create the WebSocket driver @driver = WebSocket::Driver.client(@client) # Register the on_message callback @driver.on(:message, &method(:on_message)) # Start the WebSocket handshake @driver.start # Initialize the protocol to get the page events send_command_and_wait_for_result("Page.enable") end |
Instance Method Details
#close ⇒ Object
83 84 85 86 |
# File 'lib/palapala/renderer.rb', line 83 def close @driver.close @client.close end |
#current_id ⇒ Object
Get the current ID
33 |
# File 'lib/palapala/renderer.rb', line 33 def current_id = @id |
#html_to_pdf(html, params: {}) ⇒ Object
Convert HTML content to PDF See chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
76 77 78 79 80 81 |
# File 'lib/palapala/renderer.rb', line 76 def html_to_pdf(html, params: {}) send_command_and_wait_for_event("Page.navigate", params: { url: data_url_for_html(html) }, event_name: "Page.frameStoppedLoading") result = send_command_and_wait_for_result("Page.printToPDF", params:) Base64.decode64(result["data"]) end |
#next_id ⇒ Object
Update the current ID to the next ID (increment by 1)
30 |
# File 'lib/palapala/renderer.rb', line 30 def next_id = @id = (@id || 0) + 1 |
#on_message(e) ⇒ Object
Callback to handle the incomming WebSocket messages
24 25 26 27 |
# File 'lib/palapala/renderer.rb', line 24 def (e) puts "Received: #{e.data[0..64]}" if Palapala.debug @response = JSON.parse(e.data) # Parse the JSON response end |
#process_until(&block) ⇒ Object
Process the WebSocket messages until some state is true
36 37 38 39 40 41 42 |
# File 'lib/palapala/renderer.rb', line 36 def process_until(&block) loop do @driver.parse(@client.read) return if block.call return if @driver.state == :closed end end |
#send_and_wait(message) ⇒ Object
Method to send a message (text) and wait for a response
45 46 47 48 49 |
# File 'lib/palapala/renderer.rb', line 45 def send_and_wait(, &) puts "\nSending: #{}" if Palapala.debug @driver.text() process_until(&) end |
#send_command(method, params: {}, &block) ⇒ Object
Method to send a CDP command and wait for some state to be true
52 53 54 |
# File 'lib/palapala/renderer.rb', line 52 def send_command(method, params: {}, &block) send_and_wait(JSON.generate({ id: next_id, method:, params: }), &block) end |
#send_command_and_wait_for_event(method, event_name:, params: {}) ⇒ Object
Method to send a CDP command and wait for a specific method to be called
66 67 68 69 70 |
# File 'lib/palapala/renderer.rb', line 66 def send_command_and_wait_for_event(method, event_name:, params: {}) send_command(method, params:) do @response && @response["method"] == event_name end end |
#send_command_and_wait_for_result(method, params: {}) ⇒ Hash
Method to send a CDP command and wait for the matching event to get the result
58 59 60 61 62 63 |
# File 'lib/palapala/renderer.rb', line 58 def send_command_and_wait_for_result(method, params: {}) send_command(method, params:) do @response && @response["id"] == current_id end @response["result"] end |