wch / webshot

Take screenshots of web pages from R
http://wch.github.io/webshot/
228 stars 40 forks source link

Margin/empty page when generating pdf #28

Open fabiangehring opened 8 years ago

fabiangehring commented 8 years ago

When using webshot to generate a pdf from a plotly (using latest CRAN 3.6.0) diagram I get a margin right and at the bottom and a second empty page. I don't get the margin nor the empty page when creating a png. A pdf is preferred since it's vectorised. This has direct implications when using knitr reports. The margin makes it difficult to size the diagram within the report. Png is no alternative here because of worse quality.

I wasn't able to suppress the margin with current dev. If there are possibilities, I'd glad to know. If there's none it would help a lot if this could be implemented.

library(plotly)
library(webshot)
data <- data.frame(x = c(1,2,3,4), y = c(5,4,5,7), 
                   date = c("20160101", "20160102", "20160103", "20160104"))

plot <- plot_ly(data = data, 
                x = x,
                y = y,
                color = as.character(date),
                type = "bar") %>%
  layout(title = "graphicTitle",
         hovermode = "closest")

htmlwidgets::saveWidget(as.widget(plot), "test.html")
webshot::webshot("test.html", "test.pdf") # ugly margin at bottom and right
webshot::webshot("test.html", "test.png") # no margin as expected, but poor quality since not vectorized
fabiangehring commented 8 years ago

If there is no quick fix, is there a "logic" what dimensions (and margin) there will be, when generating a pdf. i.e. 1.5 times viewport or similar. This would make it easier to cut the image afterwards?

wch commented 8 years ago

I'm not sure offhand exactly what the problem is. It might have something to do with this: https://vnese.wordpress.com/2013/07/31/casperjs-captures-pdf-of-a-page-with-media-print/

wch commented 8 years ago

When I open the .pdf file in Chrome and hit Ctrl-P, it also wants to print two pages, even though it uses a completely different rendering engine from PhantomJS. I wonder if there are some hidden elements that are making plotly graphs want a second page when printed. If so, it may be fixable with @media print of the plot's CSS, but the fix would have to be in plotly. cc @cpsievert

cpsievert commented 8 years ago

Sorry, I won't be much help here, but @timelyportfolio might know more, or could suggest a way to take advantage of the native plotlyjs snapshot support

fabiangehring commented 7 years ago

hi, I just came accross this issue again. Unfortunately this does not seem to be a plotly issue. It seems to happen with pdf-outputs in general.

The following mre shows 1) margins and 2) does not seem to respect the selector

data <- data.frame(a = 1:9, b = c(5, 2, 3, 9, 4, 3, 1, 5, 3))
table <- datatable(data)
htmlwidgets::saveWidget(widget = table, file = "table.html")
webshot("table.html", file = "table.pdf", selector = "#DataTables_Table_0", expand = 10, zoom = 2)

Screenshot: image

If this could be fixed that'd be great.

talgalili commented 6 years ago

Bump. I just came by this issue, and it seems to persist. Any chance this could be resolved?

Thanks!

dasiav7 commented 5 years ago

I also got this problem in my work. Can it be resolved? Thank You.

cpsievert commented 5 years ago

Use the new orca() function. Note the height/width is in pixels, but you can convert to inches by multiplying by 96 (assuming you're using a typical display)

wcerfgba commented 2 years ago

I am also having this problem. I have tried to specify

vwidth = 8.25 * 96,
vheight = 11.75 * 96,

and either cliprect = "viewport" or cliprect = c(0, 0, 8.25 * 96, 11.75 * 96) but I am unable to get webshot to generate a PDF with pages of the correct size. They always end up very tall with lots of blank space at the bottom.

I have chosen these numbers to produce a PDF with A4 pages at 96dpi.

Is there some way to force webshot to not create PDF pages larger than a certain size?