Skip to content

Export Slides to PDF

John Helveston edited this page Feb 18, 2021 · 20 revisions

With the pagedown package and Chrome

If you have installed Google Chrome or Chromium, you can use the function pagedown::chrome_print() to print an HTML page to PDF:

install.packages(c("pagedown", "xaringan"))
# make sure you have pagedown >= 0.2 and xaringan >= 0.9; if not, run
# remotes::install_github(c('rstudio/pagedown', 'yihui/xaringan'))

pagedown::chrome_print("path/to/your/slides.Rmd")

# or just pass the HTML output file path to chrome_print()
pagedown::chrome_print("path/to/your/slides.html")

With the xaringanBuilder package and Chrome

Install the package

# install.packages("remotes")
remotes::install_github("jhelvy/xaringanBuilder")

library(xaringanBuilder)

Build a pdf file from a Rmd or html file:

build_pdf("slides.Rmd")
build_pdf("slides.html")

Include "Complex" or partial slides:

“Complex” slides are slides that contain panelsets or other html widgets / advanced features that might not render well as a pdf. To render these, set complex_slides = TRUE. If you want to build a new slide for each increment on incremental slides, set partial_slides = TRUE.

build_pdf("slides.Rmd", complex_slides = TRUE, partial_slides = TRUE)
build_pdf("slides.html", complex_slides = TRUE, partial_slides = TRUE)

With the webshot package

Note: this has only been tested on Windows (10), MacOS & Linux right now.

The webshot() function from the {webshot} package can render a xaringan html in pdf.

Here's how to:

Install the package

# install.packages("webshot")
library(webshot)

Be sure to have PhantomJS installed

To run the first time you use the package:

install_phantomjs()

Convert to pdf

The webshot() function can convert using a link to a local html. To do this, you'll use the file protocol. Here's a simple way to point to your local file:

file_name <- paste0("file://", normalizePath("my_xaringan.html"))

You can then:

webshot(file_name, "mypdf.pdf")

Be sure to have the .pdf extension to your destination filename

Manual conversion via Web Browser

When browsing the remark repo, various approaches exist converting HTML slides to PDF. One way is to print the slides to PDF using your web browser (e.g. Ctrl + P or Command + P in Google Chrome). Firefox is not supported, Chrome should work best. There used to be some aesthetic issues, but they have been mostly solved (#65).

Automated conversion using decktape

You may also consider automated solutions like decktape.js. Please follow the README instructions to install it.

You just need to make one command-line call (or use system() to stay in R) which looks as follows:

system("`npm bin`/decktape remark slides.html slides.pdf") # decktape v2.5.0

As of more recent (xaringan vx.x.x ?) versions, xaringan provides a wrapper to this conversion, and one can simply call:

xaringan::decktape()