rstudio / webshot2

Take screenshots of web pages from R

Home Page:https://rstudio.github.io/webshot2/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Saving to PDF defaults to letter paper size / adds vertical white space

billelev opened this issue · comments

When I try to save a plotly plot as a PDF the page defaults to letter size. Is there a way to specifically control the PDF size e.g. 10 x 5 inches? There is no extra white space when saving as a PNG, and I can set the pixel size exactly.

e.g.

library(plotly)
library(htmlwidgets)
library(webshot2)

fig <- plot_ly(economics, x = ~pop, type = 'histogram')
htmlwidgets::saveWidget(fig, file="temp.html")

width <- 10 * 100
height <- 5 * 100

webshot2::webshot("temp.html", "temp.pdf", vwidth = width, vheight = height) 
webshot2::webshot("temp.html", "temp.png", vwidth = width, vheight = height) 

pdf vs png

webshot2::webshot calls a function from the chromote package to create the PDF. That function accepts arguments to change the paper size:

https://github.com/rstudio/chromote/blob/e21c70c0e56a53563f7abe6072105fe0d10cf7f6/R/screenshot.R#L202-L213

but the defaults are used:

s$screenshot_pdf(filename = file, wait_ = FALSE)

So I think the answer is that you can't do that with webshot2::webshot.

It does seem to be that way, thank you. I think the best option to get a PDF is to create a PNG and then convert to PDF.

@billelev - webshot respects your stated vwidth and vheight even with PDF output.

@ant-durrant I agree webshot does, however it adds a lot of white space. To get around this I can tweak the zoom argument, but the zoom value I need varies from machine to machine. e.g. see this and this question on Stack Overflow. I have not found a consistent way to create a trimmed PDF consistently on any machine using either webshot or webshot2.