New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formattable webshots #26

smbache opened this Issue Sep 11, 2015 · 5 comments


None yet
6 participants
Copy link

smbache commented Sep 11, 2015

Made this simple function to be able to put a formattable in a pdf report; not sure if something like this is useful in the package itself. It exports a formattable as e.g. png. Possibly something more generic would be useful in the webshot package (see also wch/webshot#1). Also it has the external dependency on phantomjs. But in any case, I thought I'd share it here:

#' Export a Formattable as PNG, PDF, or JPEG
#' @param f A formattable.
#' @param file Export path with extension .png, .pdf, or .jpeg.
#' @param width Width specification of the html widget being exported.
#' @param height Height specification of the html widget being exported.
#' @param background Background color specification.
#' @param delay Time to wait before taking webshot, in seconds.
#' @importFrom formattable as.htmlwidget
#' @importFrom htmltools html_print
#' @importFrom webshot webshot
#' @export
export_formattable <- function(f, file, width = "100%", height = NULL, 
                               background = "white", delay = 0.2)
  w <- as.htmlwidget(f, width = width, height = height)
  path <- html_print(w, background = background, viewer = NULL)
  url <- paste0("file:///", gsub("\\\\", "/", normalizePath(path)))
          file = file,
          selector = ".formattable_widget",
          delay = delay)

This comment has been minimized.

Copy link

renkun-ken commented Sep 11, 2015

It looks really interesting and can be helpful to make it work in pdf. Thanks!


This comment has been minimized.

Copy link

timelyportfolio commented Sep 11, 2015

Very nice, will try on my Windows machine. Related similar function wch/webshot#10. Would think add webshot as suggestion, so not required.


This comment has been minimized.

Copy link

kasal commented Oct 26, 2016

Thank you, this enabled me to save the table so that it can be used in a report.
But I need pdf output and file='xxx.pdf' loses most of the formatting, because of issue #53.
So I had to modify this function: save the table as an image (png) and then convert it to pdf. (Using im.convert from package 'animation'.)

It would be nice if we had a solution for issue #53, to get a real pdf instead of a wrapped image.


This comment has been minimized.

Copy link

dlksmc commented Oct 18, 2017

I'm using wkhtmltopdf to convert from HTML (generated with formattable and knitr) to PDF. Works pretty well after adding the CSS below when knitting. It aligns the column headers properly when the table spans multiple pages.

thead {
  display: table-header-group;

tr {
  page-break-inside: avoid;

This comment has been minimized.

Copy link

laresbernardo commented Nov 16, 2018

Any new update on this function? Why can't we simply run the following code?


Please, add a function or an easy way to export the results into an image without having to install phantomjs and further stuff. Hope you can help us. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment