8

Is it possible to access the "topage" variable within the main content body?

I know you can do it in headers and footers but in this specific use case, I need to put a sentence within the body that says:

"This document contains XX pages".

Heinrich Lee Yu
  • 1,472
  • 15
  • 31
  • 1
    Pretty sure this technique works even in the body, let me know if it doesn't: https://github.com/mileszs/wicked_pdf#page-numbering – Unixmonkey Sep 15 '14 at 19:21
  • 1
    It doesn't. I think what `wkhtmltopdf` does is: render the body, then render the header/footer per page with the page, no. of pages, etc. taken from the previous step. The header/footer have reserved spaces on the page so it doesn't introduce new pages when rendering the header/footer. – Heinrich Lee Yu Sep 17 '14 at 00:59
  • Well if you ever wanted to switch to something else, this example shows exactly that: http://www.cloudformatter.com/CSS2Pdf.CustomTipsTricks.HeaderFooter – Kevin Brown Oct 01 '14 at 20:09
  • Have you found a solution for that? – Seb33300 Mar 15 '16 at 14:41

1 Answers1

2

This works to me. But need to be in the header (ou footer) In the body I think it is not possible, like they said in the GitHub http://github.com/wkhtmltopdf/wkhtmltopdf/issues/1889

To put this info in the footer you can see @Unixmonkey in this link: https://github.com/mileszs/wicked_pdf#page-numbering

<html>
  <head>
    <script>
      function number_pages() {
        var vars={};
        var x=document.location.search.substring(1).split('&');
        for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = decodeURIComponent(z[1]);}
        var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
        for(var i in x) {
          var y = document.getElementsByClassName(x[i]);
          for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
        }
      }
    </script>
  </head>
  <body onload="number_pages()">
    Page <span class="page"></span> of <span class="topage"></span>
  </body>
</html>
cmnardi
  • 1,051
  • 1
  • 13
  • 27
  • 1
    `document.location.search` is only defined for the header and footer sections of the page. The main content just has an empty string there :( – Will Hitchcock Nov 16 '16 at 18:22
  • maybe it is not possible https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1889 "Nope, that can't be done -- it would possibly change the layout of the page, possibly generating new pages ... and so on." – cmnardi Nov 16 '16 at 19:27