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

1.9k views Asked by At

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.)

2

There are 2 answers

2
RLesur On BEST ANSWER

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
  )
)
5
MrFlick On

I got it to work with a custom CSS file. I created a file called custom.css and included in that file was

@page  {
  @bottom-right {
    content: counter(page);
  }
}

Then I used that along with the other pagedown defaults with a header like this

title: "My Report"
output:
  pagedown::html_paged: 
    css: ["custom.css", "default-fonts", "default"]