2017-01-12 106 views
3

我使用来生成HTML报告。我在受限制的机器上,无法安装tex。所以,我试图生成一个HTML文档,然后将其转换/打印为pdf。这个例子降价文件是:在html中将html保存为pdf

--- 
title: "trials" 
author: "Foo Bar" 
date: "15 December 2016" 
output: html_document 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
``` 

```{r cars, echo=FALSE, cache=FALSE, message=FALSE} 

library(dplyr, quietly = TRUE) 
library(abind, quietly = TRUE) 
virginica <- iris %>% filter(Species == "virginica") %>% head() %>% select(-Species) 
setosa <- iris %>% filter(Species == "setosa") %>% head() %>% select(-Species) 

diff_mat <- virginica - setosa 


diff_mat[diff_mat<0] <- '<font color="green">&dArr; </font>' 
diff_mat[diff_mat>0] <- '<font color="red">&uArr; </font>' 
diff_mat[diff_mat == 0] <- '<font color="blue">&hArr; </font>' 

datArray <- abind::abind(virginica, diff_mat, along=3) 

fin_dat <- apply(datArray,1:2, function(x)paste(x[1],x[2], sep = " ")) 

knitr::kable(fin_dat, format = "html", 
     escape = FALSE, table.attr = "border=1", 
     caption = "Changes across species") 

``` 

我不能织字无论是作为在HTML formatted tables in rmarkdown word document讨论的格式都将丢失。生成的HTML正是我想要的。使用保存在Word中的HTML语言大多数都可以处理,但我可以打印PDF,但不如直接从PDF打印。 enter image description here

当我尝试将其保存为PDF格式的PDF时,颜色丢失。

enter image description here

有一个在打印选项没有任何问题 如我们敬爱的网站Replace NA's using data from Multiple Columns打印精细

enter image description here

这个问题enter image description here

其他网页你有任何指针哪里我错过了一个观点或问题出在哪里。

回答

4

的YAML头之后加入这一权利:

<style> 
@media print { 

    font[color="green"] { 
    color: #00ff00!important; 
    -webkit-print-color-adjust:exact; 
    } 

    font[color="red"] { 
    color: #ff0000!important; 
    -webkit-print-color-adjust:exact; 
    } 

} 
</style> 

的问题是,RStudio的默认[R降价模板使用自举和他们的bootstrap.min.css版本:

@media print { 
    *, 
    *:before, 
    *:after { 
    color: #000 !important; 
    text-shadow: none !important; 
    background: transparent !important; 
    -webkit-box-shadow: none !important; 
      box-shadow: none !important; 
    } 

。这是一个相当“破坏性”的媒体查询,因为*的原因是这些设置将应用于所有标记和color: #000 !important;意味着“对您没有颜色!”当您打印文档时,请打印。我赞同这种观点(拯救地球+墨粉/墨水成本),但如果您打印到PDF,这是没有任何意义的。

不幸的是,没有针对打印为PDF的超目标媒体查询,因此当您将网页打印到PDF时这些通用的“打印”查询得到应用,而这些无意识的,全面的媒体查询接管。

您的问题在于,您需要非常明确地指定任何其他标记来覆盖这些设置。这意味着将您自己的CSS类添加到您在Rmds中生成的任何内容,或者使用“检查元素”来获得舒适感,直到您抓住所有内容为止。

然而,如果你喜欢冒险的感觉,你可以修改YAML标题是:

output: 
    html_document: 
    self_contained: false 

当您呈现到HTML,它会在各个组件VS base64-创建子目录的目录将它们编码成一个大文件。

我给我的文档forso.Rmd命名,这意味着它创建了一个名为forso_files的目录,并在其下放置了子目录。

打开主HTML文件,直到你看到这样向下滚动:

<script src="forso_files/jquery-1.11.3/jquery.min.js"></script> 
<meta name="viewport" content="width=device-width, initial-scale=1" /> 
<link href="forso_files/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 
<script src="forso_files/bootstrap-3.3.5/js/bootstrap.min.js"></script> 
<script src="forso_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script> 
<script src="forso_files/bootstrap-3.3.5/shim/respond.min.js"></script> 
<script src="forso_files/navigation-1.1/tabsets.js"></script> 

更改此:

<link href="forso_files/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 

到:

<link href="forso_files/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet" /> 

编辑bootstrap.css,取出color: #000 !important;行并添加-webkit-print-color-adjust:exact;行。因为它会在未来的渲染中被压缩(即你需要在每个渲染中复制它),所以保存bootstrap.css的副本。

你不能所有的标签由于只是链接到一个不太脑死亡印刷媒体查询一个单独的CSS文件,因为color: #000 !important;影响到*目标,你不能只是将其重置为initial或inherit`,因为这将把它们变成黑色。

您的最终(也许是最好的)选项是制作您自己的R Markdown模板(有关更多信息,请参阅https://github.com/hrbrmstr/markdowntemplates),并避免在其中放置顶级的打印介质查询。

+1

谢谢!这是我在SO中读到的最深入的答案之一。 – discipulus