wkhtmltopdf: show content on footer, for example page number
Asked Answered
F

2

5

I expected this command to show 1/1 at the bootom of the generated pdf but no... any idea?

wkhtmltopdf --footer-center [page]/[topage] www.google.com /tmp/foobar.pdf

Version: 0.12.2.4 on Linux

Fireresistant answered 5/1, 2017 at 18:58 Comment(3)
what happens? Only show '/' or show nothing?Chalcopyrite
@RafaelDantas shows nothingFireresistant
For test purpose, use --header-center and see if working. (Try put double quote in attribute text)Chalcopyrite
T
6

I think this issue may be due to version 0.12.2.4 otherwise, this --footer-center [page]/[topage] command will be doing your work.

one more example i have checked that substitutePdfVariables() is called in body onload.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script>
        function substitutePdfVariables() {

            function getParameterByName(name) {
                var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
                return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
            }

            function substitute(name) {
                var value = getParameterByName(name);
                var elements = document.getElementsByClassName(name);

                for (var i = 0; elements && i < elements.length; i++) {
                    elements[i].textContent = value;
                }
            }

            ['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']
                .forEach(function(param) {
                    substitute(param);
                });
        }
    </script>
</head>
<body onload="substitutePdfVariables()">
    <p>Page <span class="page"></span> of <span class="topage"></span></p>
</body>
</html>

Here Docs You can find out more variables about header and footer.

Tape answered 16/1, 2017 at 4:52 Comment(2)
Docs seems outdated...and guess regarding linux version seems similar to earlier answer.Anglice
@AmberBeriwal , I have rechanged the docs hyperlink. I think from this answer, user can get their answer.Tape
I
2

Seems like a stability issue but there has not been a stable release of the version 0.12.2.4 for linux (debian or ubuntu) but just for the debugging purposes as mentioned in their repositories here.

Here is the working screen shot for the version and 0.12.4

Version 0.12.4foobar1

or you can add the page number by the following snippet to add the footer as mentioned here

  <html><head><script>
  function subst() {
    var vars={};
    var x=window.location.search.substring(1).split('&');
    for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(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 style="border:0; margin: 0;" onload="subst()">
  <table style="border-bottom: 1px solid black; width: 100%">
    <tr>
      <td class="section"></td>
      <td style="text-align:right">
        Page <span class="page"></span> of <span class="topage"></span>
      </td>
    </tr>
  </table>
  </body></html>
Inlet answered 10/1, 2017 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.