【问题标题】:Why is pagedown::chrome_print() converting a solid color html element to a gradient?为什么 pagedown::chrome_print() 将纯色 html 元素转换为渐变?
【发布时间】:2021-12-24 05:17:19
【问题描述】:

我正在使用datadrivencv R 包来生成简历。在 html 中呈现默认简历可以正常工作,但是当我使用提供的代码(见下文)打印到 pdf 时,侧边栏从纯色变为渐变。如果您将 dd_cv.css 文件中的 --sidebar-background-color: 调整为更暗的东西,例如--sidebar-background-color: #727273;,这更容易看出。有没有办法防止这种情况发生?

datadrivencv::use_datadriven_cv(
  full_name = "Nick Strayer",
  data_location = "https://docs.google.com/spreadsheets/d/14MQICF2F8-vf8CKPF1m4lyGKO6_thG-4aSwat1e2TWc",
  pdf_location = "https://github.com/nstrayer/cv/raw/master/strayer_cv.pdf",
  html_location = "nickstrayer.me/cv/",
  source_location = "https://github.com/nstrayer/cv"
)
datadrivencv::use_csv_data_storage()

要渲染 cv,您必须进入 cv.rmd 文件并在第 25 行将 data_location 更改为 data_location = data/。然后运行

# This script builds both the HTML and PDF versions of your CV

# If you wanted to speed up rendering for googlesheets driven CVs you could use
# this script to cache a version of the CV_Printer class with data already
# loaded and load the cached version in the .Rmd instead of re-fetching it twice
# for the HTML and PDF rendering. This exercise is left to the reader.

# Knit the HTML version
rmarkdown::render("cv.rmd",
                  params = list(pdf_mode = FALSE),
                  output_file = "cv.html")

# Knit the PDF version to temporary html location
tmp_html_cv_loc <- fs::file_temp(ext = ".html")
rmarkdown::render("cv.rmd",
                  params = list(pdf_mode = TRUE),
                  output_file = tmp_html_cv_loc)

# Convert to PDF using Pagedown
pagedown::chrome_print(input = tmp_html_cv_loc,
                       output = "cv.pdf")

HTML截图:

PDF截图:

【问题讨论】:

  • 您检查过tmp_html_cv_loc html 文件吗?它有渐变色还是纯色?如果您无法获取临时文件,请使用 output_file = "cv_pdf.hml" 而不是 utput_file = tmp_html_cv_loc

标签: css r pdf r-markdown pagedown


【解决方案1】:

我只能在 github 文档预览中重现该问题:https://github.com/OnkarRuikar/catblock/blob/master/test/cv.pdf


但如果我在 google docs cv 等其他查看器中打开相同的 pdf,它会呈现良好。让我知道您正在使用哪个 PDF 查看器来呈现渐变?

要解决此问题,请在 dd_cv.css 中遵循 CSS 规则:

@page {
    background: 0% 0% / 100% 0.1% linear-gradient(to left, var(--sidebar-background-color) var(--sidebar-background-width), white var(--sidebar-background-width));
}

这是固定的 PDF https://github.com/OnkarRuikar/catblock/blob/master/test/cv_fixed.pdf

您会注意到语言技能栏仍然显示渐变。为此,您需要编辑cv_printing_functions.R 文件并使用类似的渐变语法修改函数print_skill_bars。或在cv.rmd 文件中为方法调用CV %&gt;% print_skill_bars(...) 提供您自己的glue_templet 参数


我在以下软件包版本上创建了 pdf:

# run following commands to get versions of installed packages required by the `datadrivencv`

 pkg = tibble::tibble(
   Package = names(installed.packages()[,3]),
   Version = unname(installed.packages()[,3])
 )
 dplyr::filter(pkg, Package %in% c("dplyr", "tidyr", "glue", "readr", "googlesheets4", "lubridate", " purrr", "stringr", "magrittr", "pagedown", "fs", "icon", "whisker", "knitr", "rmarkdown" ,"testthat"))

Following are the versions installed on my machine at the moment:
# A tibble: 15 x 2
   Package       Version
   <chr>         <chr>  
 1 dplyr         1.0.7  
 2 fs            1.5.0  
 3 glue          1.4.2  
 4 googlesheets4 1.0.0  
 5 icon          0.1.0  
 6 knitr         1.36   
 7 lubridate     1.8.0  
 8 magrittr      2.0.1  
 9 pagedown      0.15   
10 readr         2.1.0  
11 rmarkdown     2.11   
12 stringr       1.4.0  
13 testthat      3.1.0  
14 tidyr         1.1.4  
15 whisker       0.4    
 

【讨论】:

  • 感谢这解决了这个问题。我没有检查其他 pdf 查看器 - Firefox pdf 查看器显示渐变,但 Chrome 和 Foxit Reader 没有。无论哪种方式,将行添加到 css 解决了问题!我会在大约 2 小时后奖励赏金
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 2011-10-30
  • 2014-11-24
  • 1970-01-01
  • 2019-07-02
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多