rpdfr-markdownslidy

Add page numbers to pdf using pagedown::chrome_print (R package)?


I am using pagedown::chrome_print() to convert slidy presentations created with Rmarkdown to pdf -- it does a better job than saving as PDF from Chrome. However, despite studying the help file, I cannot figure out how to add page numbers. Is there a way to do this?

(Note that pagedown here refers to the R package, not the JavaScript markdown previewer.)


Solution

  • Sorry if the help page is not clear on this point.

    It is possible to pass header/footer options to Chrome using pagedown::chrome_print().

    These options are the ones defined by the Chrome DevTools Protocol for the Page.printToPDF method.

    You can customise the header and the footer with an HTML template. Chrome also offers the following values: date, title, url, pageNumber and totalPages.

    Following the explanations on this help page, here is an example to print the page numbers:

    library(htmltools)
    
    footer <- div(
      style = "font-size: 8pt; text-align: right; width: 100%; padding-right: 12pt;", 
      span(class = "pageNumber"), "/", span(class = "totalPages")
    )
    
    pagedown::chrome_print(
      "slidy.Rmd", 
      options = list(
        landscape = TRUE, 
        displayHeaderFooter = TRUE, 
        footerTemplate = format(footer, indent = FALSE),
        marginTop = 0,
        marginBottom = 0.4
      )
    )