javacsspdfflying-saucerxhtmlrenderer

With Flying Saucer, how do I generate a pdf with a page number and page total on every page at the footer?


It took me a little more research than I would have liked to figure this out on my own, so I'm going to post a comprehensive answer here. It seems like the information to do this is spread across many different websites and I'd like to put it in one place. This answer may be the same thing, but my eyes glaze over because it's in a Java string and not in a html template. Here's the question:

I'm rendering a PDF and I want a footer at the bottom of the page that says, "Page n of m" where "n" is the page number you're on and "m" is the total pages in the document. How do I do that?


Solution

  • You could also try a slightly more succinct approach:

    @page {
      @bottom-right {
        content: "Page " counter(page) " of " counter(pages);
      }
    }
    

    You need to put this in your css/style, and you do not need to add an extra footer HTML element.