1

With wkhtmltopdf, is there any way to vary the content of the footer based on the content present on the page? Each page's footer needs to have the title of the section it's in rather than a universal footer. Any pointers?

exupero
  • 9,136
  • 8
  • 47
  • 63

1 Answers1

3

The same script that works in header can also be used in footer to replace the section name. Try a footer like the one below, it grabs the current H1 section that the current page is in for the footer.

    <html>
    <head>
        <script>

            function subst() {
                var vars = {};

                var valuePairs = document.location.search.substring(1).split('&');
                for (var i in valuePairs) {
                    var valuePair = valuePairs[i].split('=', 2);
                    vars[valuePair[0]] = decodeURIComponent(valuePair[1]);
                }
                var replaceClasses = ['frompage','topage','page','webpage','section','subsection','subsubsection'];

                for (var i in replaceClasses) {
                    var hits = document.getElementsByClassName(replaceClasses[i]);

                    for (var j = 0; j < hits.length; j++) {
                        hits[j].textContent = vars[replaceClasses[i]];
                    }
                }
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body style="border:0; margin: 0px;" onload="subst()">
        <div style="width: 100%; text-align:center;">- <span class="section"></span> -</div>
    </body>
</html>

Also, if you want the H2, use subsection and for H3 use the subsubsection. If there are multiple sections or subsections per page it will pick the highest one on the page.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • Thanks! That worked perfectly! Is there any documentation on how sections are linked to header elements? – exupero Jan 11 '13 at 14:41
  • Good to hear! In the manual there is some limited information on this in http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html#Footers%20And%20Headers but other than that I really don't know, sorry! – Joel Peltonen Jan 12 '13 at 14:50