【问题标题】:Slides written in Rmd missing pages when converting from HTML to PDF从 HTML 转换为 PDF 时,用 Rmd 编​​写的幻灯片缺少页面
【发布时间】:2021-01-08 00:42:02
【问题描述】:

我正在尝试将一组用 Rmarkdown 编写并使用xaringan::moon_reader 以 HTML 格式输出的幻灯片转换为 PDF,并使用pagedown::chrome_print() 转换为 PDF。

问题是,转换后的 PDF 缺少其标题页以及整个幻灯片中的随机页面。我尝试使用 Chrome 和其他浏览器手动打印 HTML 文件以尝试保存为 PDF,但它们缺少相同的页面。

对于如何在不丢失任何页面的情况下导出为 PDF 的任何建议,我都愿意接受。不一定非要chrome_print(),只要幻灯片可以成功导出为PDF即可。

这是幻灯片的可重现伪代码:

---
title: document_title
author: author_name
output: 
  xaringan::moon_reader:
    css: [default, metropolis, metropolis-fonts] 
    lib_dir: libs
    nature:
      ratio: '4:3'
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
editor_options: 
  chunk_output_type: console
---
count: false
<style>

.center2 {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}


pre.sourceCode {
    max-height: 200px;
    overflow-y: auto;
}


/*
.remark-slide-number {
  position: inherit;
}

.remark-slide-number .progress-bar-container {
  position: absolute;
  bottom: 0;
  height: 4px;
  display: block;
  left: 0;
  right: 0;
}

.remark-slide-number .progress-bar {
  height: 100%;
  background-color: blue;
}
*/
</style>


# only using two backticks for codechunks in rmd to escape ending the codeblock on stackoverflow
``{css, echo=FALSE}

@media print {
  .has-continuation {
    display: block !important;
  }
}

``


``{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(knitr)
opts_chunk$set(
  fig.align="center",  
  fig.height=4,
  dpi=300, 
  cache=T , 
  echo=F 
  )
library(tidyverse)
library(hrbrthemes)
library(fontawesome)

``
count: true

# Slide 1

</br>
.font130[

1. Content 1

2. Content 2

]

---
count: false
# Slide 2
</br>
.font130[

Content for slide 2
]
---

``{r gen_pdf, include = FALSE, cache = FALSE, eval = TRUE}
pagedown::chrome_print("slides.html", output = "slides.pdf")
``

我们在整个文档中都有count:true count: false,但它似乎与页面是否丢失无关。

非常感谢您!

【问题讨论】:

    标签: r-markdown r-package html-to-pdf pagedown xaringan


    【解决方案1】:

    对于遇到相同问题的任何人,我已经弄清楚我的幻灯片出了什么问题:

    所以最后它确实与count: truecount: false有关

    count: true 表示页面的开始,但count: false 在同一页面上以 html 格式添加新信息,但所有中间结果都包含在您最后一个 count: false 部分的任何部分中。

    一个例子是:

    ---
    count: true 
    Content 1
    ---
    count: false
    Content 2
    ---
    count: false
    Content 3
    

    在这种情况下,内容 1 将是新页面的开始,.html 中的所有内容都会正常显示,但在转换为的 pdf 中,只会显示内容 3,而不显示内容 1 或 2。

    标题页自动设置为count: true,但如果您在其后有count: false 部分,则它不会显示。

    希望这能解释清楚!

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 2021-01-24
      • 2014-05-02
      • 2011-11-28
      • 2019-03-26
      • 2021-12-14
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多