问题描述
我正在尝试将一组用 Rmarkdown 编写并以 HTML 输出的幻灯片与 xaringan::moon_reader
转换为 PDF,我使用 pagedown::chrome_print()
转换为 PDF。
问题是,转换后的 PDF 缺少标题页以及整个幻灯片中的随机页面。我尝试使用 Chrome 和其他浏览器手动打印 HTML 文件以尝试保存为 PDF,但它们丢失了相同的页面。
我愿意接受有关如何在不丢失任何页面的情况下导出为 PDF 的任何建议。只要幻灯片可以成功导出为 PDF,就不必是 chrome_print()
。
---
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
,但这似乎与页面是否丢失无关。
在此先感谢您!
解决方法
对于遇到同样问题的人,我已经弄清楚我的幻灯片出了什么问题:
所以最终它确实与 this.cd.detectChanges();
或 count: true
count: false
表示页面的开始,但 count: true
以 html 格式在同一页面上添加新信息,但所有中间结果都包含在最后一个 {{1} }} 部分。
一个例子是:
count: false
在这种情况下,内容 1 将是新页面的开始,所有内容都会在 count: false
中正常显示,但在转换成的 pdf 上,只会显示内容 3,而不显示内容 1 或2.
标题页已自动设置为 ---
count: true
Content 1
---
count: false
Content 2
---
count: false
Content 3
,但如果您在其后面有 .html
部分,它将不会显示。
希望这能解释它!