Generate parametrized PDF report with Rmarkdown with Pagedown

I want to generate PDF reports that take in a parameter (e.g., name) from a list. I am able to generate HTML reports (see command below) but not PDF.

render_fun <- function(intern_name){
  rmarkdown::render(input = "./Reports/Report_Interns.Rmd",
                    output_file = glue::glue("report_{intern_name}"), 
                    params = list(name = intern_name, school = ""))
  
}

interns_name %>% purrr::walk(render_fun)

Note that the output_file does not contain an extension (e.g., .pdf). If I do add this extension, the compiler throws an error:

e.g.,


render_fun <- function(intern_name){
  rmarkdown::render(input = "./Reports/Report_Interns.Rmd",
                    output_file = glue::glue("report_{intern_name}.pdf"), # note the .pdf at end
                    params = list(name = intern_name, school = ""))
  
}

interns_name %>% purrr::walk(render_fun)

This throws an error:

"C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS Report_Interns.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc8d04ec57a05.pdf --lua-filter "C:\Users\J~1\AppData\Local\R\WIN-LI~1\4.3\bookdown\RMARKD~1\lua\CUSTOM~1.LUA" --lua-filter "C:\Users\J~1\AppData\Local\R\WIN-LI~1\4.3\RMARKD~1\RMARKD~1\lua\PAGEBR~1.LUA" --lua-filter "C:\Users\J~1\AppData\Local\R\WIN-LI~1\4.3\RMARKD~1\RMARKD~1\lua\LATEX-~1.LUA" --embed-resources --standalone --wrap preserve --lua-filter "C:/Users/J/AppData/Local/R/win-library/4.3/pagedown/resources/lua/uri-to-fn.lua" --lua-filter "C:/Users/J/AppData/Local/R/win-library/4.3/pagedown/resources/lua/loft.lua" --lua-filter "C:/Users/J/AppData/Local/R/win-library/4.3/pagedown/resources/lua/footnotes.lua" "--mathjax=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --metadata newpage_html_class="page-break-after" --section-divs --template "C:\Users\J~1\AppData\Local\R\WIN-LI~1\4.3\pagedown\RESOUR~1\html\PAGED~1.HTM" --highlight-style pygments --number-sections --css style.css --include-in-header "C:\Users\J~1\AppData\Local\Temp\RtmpmYEE2S\rmarkdown-str8d079967747.html" 
cannot produce pdf output from html4
Error in `map()`:
ℹ In index: 1.
Caused by error:
! pandoc document conversion failed with error 4

I suspect it's because my YAML contains a pagedown library which first generates an HTML and then converts that HTML to PDF.

---
title: "`r params$name`"
author: "`r params$school`"
date: "`r Sys.Date()`"
params:
  name: ""
  school: "School"
output:
  pagedown::html_paged:
    css: style.css
knit: pagedown::chrome_print
---

In short, how do I generate PDF reports automatically? If I manually specific name in the YAML, I do get the PDF report so I know that there's nothing wrong with the RMarkdown or pagedown.

This will work only in RStudio IDE for the knit button.

If you read the help page form pagedown::chrome_print() you'll see that when input is a Rmd file, it will first render with R Markown and then run the chrome printing. So you could wrap this function.

Otherwise, you need to

  • First render to your output format (which is HTML IIUC)
  • then call chrome_print() on the output

You can do that in your map()

here you get the error because you are calling rmarkdown::render() with default format (which is HTML) and call the output something.pdf which is not supported.

Hope this helps understand the problem.