gt icon indicating copy to clipboard operation
gt copied to clipboard

PDF Output does not support background colors

Open AlbertRapp opened this issue 2 years ago • 0 comments

Description

PDF output does not include background colors of cells (text colors are not affected by this). The problem seems to be an underlying issue with how {webshot2} aka {chromote} aka Chrome browsers print documents. It seems that by default browsers will not include background CSS (at least that's what this SO answer says).

I can confirm that when I want to print a {gt} table from Chrome manually, I will not get background colors. It is only when I activate the "Background graphics" option in the print window of Chrome, then I get my desired output. So a solution would be to let {chromote} know that it should use that setting when printing something. I couldn't find a way to pass any meaningful option like that to gtsave() to get the desired result.

Reproducible example

library(gt)
tbl <- towny |> 
  dplyr::slice(1:5) |> 
  dplyr::select(name, land_area_km2) |> 
  gt() |> 
  tab_style(
    style = cell_text(color = 'dodgerblue4'),
    locations = cells_body(columns = 1)
  ) |> 
  tab_style(
    style = list(
      cell_fill(color = 'dodgerblue4'),
      cell_text(color = 'white', weight = 'bold')
    ),
    locations = cells_body(columns = 2)
  )
gtsave(tbl, 'img.png') # Works fine
gtsave(tbl, 'img.pdf') # No Background colors

image

Created on 2023-12-01 with reprex v2.0.2

Expected result

img

Session info

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       Ubuntu 22.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Berlin
#>  date     2023-12-01
#>  pandoc   3.1.8 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.3.0)
#>  digest        0.6.33     2023-07-07 [1] CRAN (R 4.3.1)
#>  dplyr         1.1.3      2023-09-03 [1] CRAN (R 4.3.1)
#>  evaluate      0.22       2023-09-29 [1] CRAN (R 4.3.1)
#>  fansi         1.0.5      2023-10-08 [1] CRAN (R 4.3.1)
#>  fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.3.0)
#>  fs            1.6.3      2023-07-20 [1] CRAN (R 4.3.1)
#>  generics      0.1.3      2022-07-05 [1] CRAN (R 4.3.0)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.3.0)
#>  gt          * 0.10.0     2023-10-07 [1] CRAN (R 4.3.1)
#>  htmltools     0.5.6.9001 2023-10-16 [1] Github (rstudio/htmltools@9436031)
#>  knitr         1.44       2023-09-11 [1] CRAN (R 4.3.1)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.3.0)
#>  purrr         1.0.2      2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo          1.25.0     2022-06-12 [1] CRAN (R 4.3.1)
#>  R.utils       2.12.2     2022-11-11 [1] CRAN (R 4.3.1)
#>  R6            2.5.1.9000 2023-10-31 [1] Github (r-lib/R6@e97cca7)
#>  reprex        2.0.2      2022-08-17 [1] CRAN (R 4.3.0)
#>  rlang         1.1.1      2023-04-28 [1] CRAN (R 4.3.0)
#>  rmarkdown     2.25       2023-09-18 [1] CRAN (R 4.3.1)
#>  rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.3.1)
#>  styler        1.10.2     2023-08-29 [1] CRAN (R 4.3.1)
#>  tibble        3.2.1      2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4      2023-10-22 [1] CRAN (R 4.3.1)
#>  vctrs         0.6.4      2023-10-12 [1] CRAN (R 4.3.1)
#>  withr         2.5.2      2023-10-30 [1] CRAN (R 4.3.1)
#>  xfun          0.40       2023-08-09 [1] CRAN (R 4.3.1)
#>  xml2          1.3.5      2023-07-06 [1] CRAN (R 4.3.1)
#>  yaml          2.3.7      2023-01-23 [1] CRAN (R 4.3.0)
#> 
#>  [1] /home/albert/R/x86_64-pc-linux-gnu-library/4.3
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

AlbertRapp avatar Dec 01 '23 11:12 AlbertRapp