5

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

jtr13
  • 1,225
  • 11
  • 25

2 Answers2

6

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
  )
)
RLesur
  • 5,810
  • 1
  • 18
  • 53
4

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"]
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Doesn't that produce html output? – jtr13 Jan 22 '20 at 00:27
  • 1
    Well, pagedown works by making html and then printing to pdf through a web browser. At least that’s how I understand how it works. – MrFlick Jan 22 '20 at 00:31
  • I think that's right. So the idea would be to add numbers with `html_paged` (or perhaps directly through slidy options) and then use `chrome_print`? – jtr13 Jan 22 '20 at 00:36
  • I mean I used exactly this and I got page numbers in my output. Not sure if there was another way. – MrFlick Jan 22 '20 at 00:40
  • The problem is that the output that `pagedown::html_paged` produces is formatted very differently from the `slidy_presentation` output. I'm going to need to find a way to either add page numbers to the original slidy slides, or perhaps later to the pdf rendered with `pagedown::chrome_print`. – jtr13 Jan 22 '20 at 01:02