Skip to content
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

Margin/empty page when generating pdf #28

Open
fabiangehring opened this issue Aug 31, 2016 · 9 comments
Open

Margin/empty page when generating pdf #28

fabiangehring opened this issue Aug 31, 2016 · 9 comments

Comments

@fabiangehring
Copy link

fabiangehring commented Aug 31, 2016

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
Copy link
Author

fabiangehring commented Sep 6, 2016

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
Copy link
Owner

wch commented Sep 6, 2016

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
Copy link
Owner

wch commented Sep 6, 2016

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
Copy link

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
Copy link
Author

fabiangehring commented Feb 6, 2017

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
Copy link

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

Thanks!

@dasiav7
Copy link

dasiav7 commented Nov 29, 2018

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

@cpsievert
Copy link

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants