Studiosity / grover

A Ruby gem to transform HTML into PDFs, PNGs or JPEGs using Google Puppeteer/Chromium
MIT License
946 stars 107 forks source link

Custom "wait_until" option. #239

Closed Mohamed-Zkaria closed 5 months ago

Mohamed-Zkaria commented 5 months ago

Hi, I am using third party charts and it takes time to render but grover makes the file quicker. Is there a way to make pdf rendering wait until a specific element has rendered?

I tried to make a custom js event const event = new Event("map_rendering"); and use window.dispatchEvent(event); after drawing but the option 'wait_until' doesn't accept it.

abrom commented 5 months ago

wait_until only accepts one (or many) the the puppeteer lifecycle events: https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlifecycleevent.md

It sounds like from your description that you have a callback that runs when the rendering is complete? If so, you should be able to use either the wait_for_selector or wait_for_function options

wait_for_selector example:

...., ()=> {
  // callback when rendering is complete -> add a selector to an element
}

then in your Grover config (eg):

  wait_for_selector: 'body.contents-render-complete'
  ...

ie you add the contents-render-complete class to the body (or where ever you prefer) then tell grover to wait until it is present.

wait_for_function example:

...., ()=> {
  // callback when rendering is complete
  window.renderingComplete = true
}

then in your Grover config

  wait_for_function: 'window.renderingComplete === true'
  ...

ie you set a variable that grover can test for when the rendering is complete, and tell grover to wait until that variable is set.

HTH

Mohamed-Zkaria commented 5 months ago

Thank you for your response, really appreciate it. This helped.