CaptureHandler tutorial

CaptureHandler allows you to take screenshot of pages. It relies on Chrome’s puppeteer (requires node 9.X) and ensure node is in your path. It can be configured in gramex as below:

capture:
  pattern: /$YAMLURL/capture
  handler: CaptureHandler
  kwargs:
    engine: chrome  # engine
    timeout: 10     # default request timeout for capturing
    port: 9900      # default port number

Configure CaptureHandler in front-end (or) To encode URL using JavaScript:

var url = g1.url.parse(location.href); // fetch current url using g1 library
// trigger the screenshot
var capture_url = url.join('capture')
  .update({'url': location.href, 'exp': 'pdf', 'delay': 10000, 'd': 'true'})
window.open(capture_url.toString())
// or 
$('.custom-pdf').attr('href', capture_url.toString())

To encode URLs using a python template:

{% from six.moves.urllib_parse import urlencode %}
<a href="capture?{{ urlencode(url='...', header='header text') }}"

Schedule CaptureHandler

CaptureHandler can be used as a service via python. YAML configuration for which is as follows:

capture-scheduler:
  function: app.screenshot_scheduler
  dates: '-'
  hours: '2'      # 2nd hour of the day
  minutes: '30' # 30th minute

corresponding screenshot_scheduler function in app.py:

def screenshot_scheduler():
    params = {'url': 'https://learn.gramener.com/guide/', 'delay': 4000, 'ext': 'png'}
    url = 'http://127.0.0.1:8001/capture/?' + urlencode(params)
    webbrowser.open(url)

Supported Arguments

Arguments supported by CaptureHandler:

For PDF:

For PNG/JPG:

For PPTX:

For detailed documentation, visit CaptureHandler.