在html中将html保存为pdf [英] Saving html to pdf in chrome

查看:181
本文介绍了在html中将html保存为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 rmarkdown 来生成HTML报告。我在受限制的机器上,无法安装tex。所以,我试图生成一个HTML文档,然后将其转换/打印为pdf。示例markdown文档是:

  --- 
title:trials
author:Foo酒吧
日期:2016年12月15日
输出:html_document
---
$ b $```{r setup,include = FALSE}
knitr :: opts_chunk $ set(echo = TRUE)
```
$ b $```{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 ,函数(x)paste(x [1],x [2],sep =))

knitr :: kable(fin_dat,format =html,
escape = FALSE,table.attr =border = 1,
caption =种间变化)

```

我无法编写单词,因为格式化会丢失,详见



打印选项中没有问题



其他页面,例如我们心爱的网站中的这个问题



您是否有任何指向我缺少点或问题出在哪里。

解决方案

在YAML标题后面添加这个权限:

 < style> 
@media print {

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

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

}
< / style>

问题是RStudio的默认R markdown模板使用Bootstrap及其版本 bootstrap.min.css 有:

  @media print {
*,
*:之前,
*:之后{
颜色:#000!重要;
text-shadow:none!important;
背景:透明!重要;
-webkit-box-shadow:none!important;
box-shadow:none!important;
}

在里面。这是一个非常破坏性的媒体查询,因为 * 会导致这些设置应用于所有标记和颜色:#000!important; 表示当您打印文档时您没有颜色!。我赞同这种观点(拯救地球+墨粉/墨水成本),但如果您打印到PDF,这是没有任何意义的。

遗憾的是,没有针对PDF打印的超针对性媒体查询,所以当您将网页打印为PDF时,通用的打印抓住所有媒体查询接管。



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

不过,如果您觉得冒险,您可以修改YAML标头为:

 输出:
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 =viewportcontent =width = device-width,initial-scale = 1/>
< link href =forso_files / bootstrap-3.3.5 / css / bootstrap.min.css =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 =stylesheet/> 

到:

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

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



颜色:#000!important; 之后,您不能链接到一个单独的CSS文件>会影响所有标签,这要归功于 * 目标,您不能将其重置为初始或继承,因为也可以将它们变成黑色。



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


I am using rmarkdown to generate an HTML report. I am on a restricted machine, can't install tex. So, I was trying to generate an HTML document and then convert/print it to a pdf. The example markdown document is:

---
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")

```

I can't knit to word either as the formatting is lost as discussed in HTML formatted tables in rmarkdown word document. The HTML produced is exactly what I wanted. HTML to word using save as in word works mostly fine with some issues and I can print pdf but it is not as good as directly printed from pdf.

when I try to save it as pdf in chrome the colour is lost.

There is no issues in print options

Other pages such as this question in our beloved site Replace NA's using data from Multiple Columns prints fine

Do you have any pointers where I am missing a point or where the issue is.

解决方案

Add this right after the YAML header:

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

The problem is that RStudio's default R markdown templates use Bootstrap and their version of bootstrap.min.css has:

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

in it. That's a pretty "destructive" media query as the *'s cause those settings to be applied to all tags and color: #000 !important; means "no color for YOU!" when you print a document. I grok the sentiment behind that (saving the planet + toner/ink costs) but if you're printing to PDF it makes no sense whatsoever.

Unfortunately, there are no hyper-targeted media queries for printing to PDF, so the generic "print" ones get applied when you print web pages to PDFs and these mindless, catch-all media queries take over.

The problem for you is that you'll need to be very specific in targeting any other tags to override these settings. Which means adding your own CSS classes to anything you generate in Rmds or getting cozy with "Inspect Element" until you catch'em all.

However, if you're feeling adventurous you can modify the YAML header to be:

output:
  html_document:
    self_contained: false

When you render to HTML it'll create a directory with subdirectories for the various components vs base64-encode them into one big document.

I named my document forso.Rmd which means it made a directory called forso_files and put subdirs under it.

Open up the main HTML file and scroll down until you see something like:

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

Change this:

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

to:

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

Edit bootstrap.css, remove the color: #000 !important; line and add the -webkit-print-color-adjust:exact; line. SAVE A COPY OF bootstrap.css ELSEWHERE as it'll get squashed on future renders (i.e. you'll need to copy it back on every render).

You can't just link to a separate CSS file with a less brain dead print media query since the color: #000 !important; impacts all tags thanks to the * target and you can't just reset it to initial or inherit` because that will just turn them black as well.

Your final (and probably best) option is to make your own R Markdown template (see https://github.com/hrbrmstr/markdowntemplates for more info) and avoid placing over-arching print media queries in it.

这篇关于在html中将html保存为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆