rstudio / pagedown

Paginate the HTML Output of R Markdown with CSS for Print
https://pagedown.rbind.io
Other
889 stars 129 forks source link

chrome_print: Background color not plotted #287

Closed JonasGeschke closed 2 years ago

JonasGeschke commented 2 years ago

Hi, I am using chrome_print, pagedown version 0.18. I have the following settings, but unfortunately the background colors dont plot:

pagedown::chrome_print(input = output_html, output = output_pdf, format = "pdf", wait = 5, options = list(printBackground = TRUE, marginTop = 0, marginRight = 0, marginBottom = 0, marginLeft = 0, preferCSSPageSize = TRUE))

Any idea on what could cause this? In the HTML, the background colors look fine.

cderv commented 2 years ago

Can you give an example of Rmd you are trying to print ? Which background color are you trying to print exactly ? Code chunk background color ? Others elements ?

It is possible that the document you are using insert some CSS with print media rules that will remove background for printing. You may need to add some CSS rules to change that.

For example, bootstrap 3 has that behavior (https://github.com/rstudio/pagedown/issues/229, https://github.com/rstudio/pagedown/issues/233)

JonasGeschke commented 2 years ago

Many thanks for the quick response! And yes, indeed, I am sorry for not having provided a hands-on-example of my issue. Going to prepare it asap!

JonasGeschke commented 2 years ago

So here we are, I hope it is working properly...

Here you can download my example files: https://we.tl/t-6grTS0Xzz3 1st my Rmd file, with all content "texted". 2nd the output HTML, with the footnote correctly colored in "ghostwhite". 3rd the output PDF, with the footnote not colored anymore.

The code for producing the HTML: rmarkdown::render(paste0(datadir, "/onepager_layout.Rmd"), output_file = output_html, params = list(uoa = uoa), quiet = TRUE)

For the example, you can use uoa <- "CHE". The code for producing the PDF is given in my initial issue posting.

Many thanks for advice on this!

cderv commented 2 years ago

I'll have a look.

It is possible that the document you are using insert some CSS with print media rules that will remove background for printing. You may need to add some CSS rules to change that.

Did you look for those CSS rules though ?

cderv commented 2 years ago

Ok so I have looked at your HTML and you are getting exactly what I described abouve. There is a print media rule that makes all background transparent. You need to add a CSS rule to prevent that for your document. See linked issue https://github.com/rstudio/pagedown/issues/229

JonasGeschke commented 2 years ago

Alright, thanks for looking into it! After some researching I now added the following to the CSS and it works just fine:

@media print { -webkit-print-color-adjust: exact !important; } and an !important to each background color setting.

Many thanks for your input and help!!

cderv commented 2 years ago

Following my comment in https://github.com/rstudio/pagedown/issues/229#issuecomment-879059554 note that switching to bootstrap 4 or 5 would also be a solution as it allows to deactivate all the specific print style rule to have your own