| 1 |
#' Print a web page to PDF/PNG/JPEG |
| 2 |
#' |
| 3 |
#' Print a web page to PDF or take a screenshot to PNG/JPEG via a headless |
| 4 |
#' browser such as Chromium or Google Chrome. |
| 5 |
#' @param input Path or URL to the HTML page to be printed. |
| 6 |
#' @param output An output filename. If only an extension is provided, the |
| 7 |
#' filename will be inferred from `url` via [url_filename()]. Only `.pdf`, |
| 8 |
#' `.png`, and `.jpeg` are supported. |
| 9 |
#' @param args Command-line arguments to be passed to the headless browser. The |
| 10 |
#' default arguments can be found in `xfun:::browser_args()`. You may pass |
| 11 |
#' additional arguments on top of these via, e.g., `c('default', |
| 12 |
#' '--no-pdf-header-footer')`, or completely override the default arguments by |
| 13 |
#' providing a vector of other arguments. |
| 14 |
#' @param window_size The browser window size when taking a PNG/JPEG screenshot. |
| 15 |
#' Ignored when printing to PDF. |
| 16 |
#' @param browser Path to the web browser. By default, the browser is found via |
| 17 |
#' `xfun:::find_browser()`. If it cannot be found, you may set the global |
| 18 |
#' option `options(xfun.browser = )` or environment variable `R_XFUN_BROWSER` |
| 19 |
#' to the path of the browser, or simply pass the path to the `browser` |
| 20 |
#' argument. |
| 21 |
#' @return The `output` path if the web page is successfully printed. |
| 22 |
#' @export |
| 23 |
#' @examplesIf interactive() |
| 24 |
#' xfun::browser_print('https://www.r-project.org') |
| 25 |
browser_print = function( |
| 26 |
input, output = '.pdf', args = 'default', window_size = c(1280, 1024), |
| 27 |
browser = NULL |
| 28 |
) { |
| 29 |
browser = check_browser(browser) |
| 30 |
if (sans_ext(output) == '') output = with_ext(url_filename(input), output) |
| 31 |
to_pdf = tolower(file_ext(output)) == 'pdf' |
| 32 |
if (!to_pdf && !any(grepl('--window-size=', args))) |
| 33 |
args = c(args, paste0('--window-size=', paste(window_size, collapse = ','))) |
| 34 |
if ('default' %in% args) { |
| 35 |
args = setdiff(c( |
| 36 |
args, browser_args(), |
| 37 |
sprintf('--%s="%s"', if (to_pdf) 'print-to-pdf' else 'screenshot', normalize_path(output)), |
| 38 |
shQuote(input) |
| 39 |
), 'default') |
| 40 |
} |
| 41 |
if (system2(browser, args, stderr = FALSE) != 0) stop('Failed to print to ', output) |
| 42 |
output |
| 43 |
} |
| 44 |
|
| 45 |
#' Execute JavaScript on an HTML page via a headless browser and dump the DOM |
| 46 |
#' |
| 47 |
#' Load an HTML page in a headless browser (Chromium/Chrome/Edge), execute all |
| 48 |
#' JavaScript on it, and return (or save) the rendered DOM as HTML. |
| 49 |
#' @param input Path or URL to the HTML page. |
| 50 |
#' @param output An optional output file path. If not provided, the rendered |
| 51 |
#' HTML is returned as a character string. |
| 52 |
#' @param fragment Whether to return only the inner body content instead of the |
| 53 |
#' full HTML document. |
| 54 |
#' @inheritParams browser_print |
| 55 |
#' @return If `output` is `NULL`, the rendered HTML as a character string; |
| 56 |
#' otherwise the `output` path (invisibly). |
| 57 |
#' @export |
| 58 |
#' @examplesIf interactive() |
| 59 |
#' f = tempfile(fileext = '.html') |
| 60 |
#' writeLines(c( |
| 61 |
#' '<html><body><div id="out"></div>', |
| 62 |
#' '<script>document.getElementById("out").textContent = "hello";</script>', |
| 63 |
#' '</body></html>' |
| 64 |
#' ), f) |
| 65 |
#' xfun::browser_dom(f) |
| 66 |
browser_dom = function(input, output = NULL, fragment = FALSE, browser = NULL) { |
| 67 |
browser = check_browser(browser) |
| 68 |
args = c(browser_args(), '--dump-dom', '--log-level=3', input) |
| 69 |
html = system2(browser, args, stdout = TRUE, stderr = FALSE) |
| 70 |
if (!is.null(attr2(html, 'status'))) stop('Failed to dump DOM.') |
| 71 |
html = gsub('\\s*<script[^>]*>(?s).*?</script>\\s*', '', one_string(html), perl = TRUE) |
| 72 |
if (fragment) |
| 73 |
html = gsub('^(?s).*?<body[^>]*>\\s*|\\s*</body>(?s).*$', '', html, perl = TRUE) |
| 74 |
if (is.null(output)) raw_string(html) else write_utf8(html, output) |
| 75 |
} |
| 76 |
|
| 77 |
check_browser = function(browser) { |
| 78 |
if (is.null(browser)) browser = env_option('xfun.browser', find_browser()) |
| 79 |
if (!file.exists(browser)) browser = Sys.which(browser) |
| 80 |
if (!utils::file_test('-x', browser)) stop('The browser is not executable: ', browser) |
| 81 |
browser |
| 82 |
} |
| 83 |
|
| 84 |
browser_args = function() c( |
| 85 |
proxy_args(), if (is_windows()) '--no-sandbox', '--headless', |
| 86 |
'--no-first-run', '--no-default-browser-check', '--hide-scrollbars', |
| 87 |
# avoid spawning the component/CRX URL fetcher, which leaves temp files |
| 88 |
# (e.g., 'com.google.Chrome.chromecrx_chrome_url_fetcher_*') behind |
| 89 |
'--disable-background-networking', '--disable-component-update' |
| 90 |
) |
| 91 |
|
| 92 |
find_browser = function() { |
| 93 |
switch( |
| 94 |
.Platform$OS.type, |
| 95 |
windows = { |
| 96 |
res = unlist(lapply(c('ChromeHTML', 'MSEdgeHTM'), function(x) { |
| 97 |
res = tryCatch({ |
| 98 |
unlist(utils::readRegistry(sprintf('%s\\shell\\open\\command', x), 'HCR')) |
| 99 |
}, error = function(e) '') |
| 100 |
res = unlist(strsplit(res, '"')) |
| 101 |
res = head(res[file.exists(res)], 1) |
| 102 |
})) |
| 103 |
if (length(res) == 0) stop( |
| 104 |
'Cannot find Google Chrome or Edge automatically from the Windows Registry Hive. ', |
| 105 |
"Please pass the full path of chrome.exe or msedge.exe to the 'browser' argument ", |
| 106 |
"or to the environment variable 'R_XFUN_BROWSER'." |
| 107 |
) |
| 108 |
res[1] |
| 109 |
}, |
| 110 |
unix = { |
| 111 |
app_path = function(app) sprintf('/Applications/%s.app/Contents/MacOS/%s', app, app) |
| 112 |
for (i in c( |
| 113 |
app_path(c('Google Chrome', 'Microsoft Edge')), |
| 114 |
'google-chrome', 'chromium-browser', 'chromium', 'google-chrome-stable', |
| 115 |
'microsoft-edge-stable', 'microsoft-edge')) { |
| 116 |
if ((res <- Sys.which(i)) != '') break |
| 117 |
} |
| 118 |
if (res == '') stop('Cannot find Chromium, Google Chrome, or Edge') |
| 119 |
res |
| 120 |
}, |
| 121 |
stop('Your platform is not supported') |
| 122 |
) |
| 123 |
} |
| 124 |
|
| 125 |
proxy_args = function() { |
| 126 |
x = Sys.getenv(c('https_proxy', 'HTTPS_PROXY', 'http_proxy', 'HTTP_PROXY')) |
| 127 |
x = x[x != ''] |
| 128 |
if (length(x) == 0) return() |
| 129 |
c( |
| 130 |
paste0('--proxy-server=', x[1]), |
| 131 |
shQuote(paste0('--proxy-bypass-list=', paste(no_proxy(), collapse = ';'))) |
| 132 |
) |
| 133 |
} |
| 134 |
|
| 135 |
no_proxy = function() { |
| 136 |
x = do.call(c, strsplit(Sys.getenv(c('no_proxy', 'NO_PROXY')), '[,;]')) |
| 137 |
x = c('localhost', '127.0.0.1', x) |
| 138 |
unique(x) |
| 139 |
} |
| 140 |
|