How do I get total pages in wkhtmltopdf body?
Asked Answered
B

1

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

Berna answered 11/9, 2014 at 10:21 Comment(4)
Pretty sure this technique works even in the body, let me know if it doesn't: github.com/mileszs/wicked_pdf#page-numberingKalin
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.Berna
Well if you ever wanted to switch to something else, this example shows exactly that: cloudformatter.com/CSS2Pdf.CustomTipsTricks.HeaderFooterCollision
Have you found a solution for that?Jennie
I
3

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>
Ingulf answered 19/9, 2016 at 21:43 Comment(2)
document.location.search is only defined for the header and footer sections of the page. The main content just has an empty string there :(Ormandy
maybe it is not possible 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."Ingulf

© 2022 - 2024 — McMap. All rights reserved.