Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get total pages in wkhtmltopdf body?

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

like image 476
Heinrich Lee Yu Avatar asked Sep 11 '14 10:09

Heinrich Lee Yu


1 Answers

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>
like image 144
cmnardi Avatar answered Oct 18 '22 20:10

cmnardi