browser_print: Print a web page to PDF/PNG/JPEG

View source: R/browser.R

browser_printR Documentation

Print a web page to PDF/PNG/JPEG

Description

Print a web page to PDF or take a screenshot to PNG/JPEG via a headless browser such as Chromium or Google Chrome.

Usage

browser_print(
  input,
  output = ".pdf",
  args = "default",
  window_size = c(1280, 1024),
  browser = NULL,
  query = NULL,
  wait = NULL,
  timeout = 60
)

Arguments

input

Path or URL to the HTML page to be printed.

output

An output filename. If only an extension is provided, the filename will be inferred from url via url_filename(). Only .pdf, .png, and .jpeg are supported.

args

Command-line arguments to be passed to the headless browser. The default arguments can be found in xfun:::browser_args(). You may pass additional arguments on top of these via, e.g., c('default', '--force-device-scale-factor=2'), or completely override the default arguments by providing a vector of other arguments.

window_size

The browser window size when taking a PNG/JPEG screenshot. Ignored when printing to PDF.

browser

Path to the web browser. By default, the browser is found via xfun:::find_browser(). If it cannot be found, you may set the global option options(xfun.browser = ) or environment variable R_XFUN_BROWSER to the path of the browser, or simply pass the path to the browser argument.

query

A named list (or vector) of query parameters to be appended to the URL of the page, e.g., list(paged = 1). If input is a file path, it will be converted to a ⁠file://⁠ URL first.

wait

A JavaScript expression that must be true before the page is printed (or its DOM is dumped), e.g., "window.ready". It is evaluated repeatedly until it returns a true value or the time is out. If it returns a Promise, the page is ready when the promise is resolved (regardless of its value). To wait for an event on window instead, use a named vector (or list) with the name of the event, and optionally a trigger event to be dispatched first to request something from the page, e.g., c(event = "ready") or c(event = "pagesjs:after", trigger = "pagesjs:paginate"). The page is always waited for until it is loaded. The command-line flags of the browser can only wait for a fixed time, so query and wait make the function control the browser via the Chrome DevTools Protocol instead, which is more reliable for pages that update themselves after they are loaded.

timeout

The maximum number of seconds to wait for the browser and the page (when query or wait is used).

Value

The output path if the web page is successfully printed.

Examples


xfun::browser_print("https://www.r-project.org")

# wait for a condition, or an event, on the page
# xfun::browser_print("index.html", wait = "window.pageReady")
# xfun::browser_print("index.html", wait = c(event = "pageReady"))


xfun documentation built on Nov. 30, 2026, 9:06 a.m.

Related to browser_print in xfun...