Kuanysh Yerezhep
Kuanysh Yerezhep

Reputation: 21

How to disable --footer-html on first 3 pages wkhtmltopdf?

I'm generating pdf with wkhtmltopdf. There is a problem, that I cannot disable --footer-html on first THREE pages.

Below is java code for generating pdf:

        pdf.addPageFromString(parseThymeleafTemplate());
        pdf.addParam(new Param("--page-size", "A4", "-B", "35mm", "-L", "0", "-R", "0", "-T", "0"));
        pdf.addParam(new Param("--footer-html", "/Users/kuanysh/IdeaProjects/pdf-report-sender/src/main/resources/templates/footer.html"));

And my footer.html

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script>
        function subst() {
            var vars = {};
            var x = document.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]];

                if (vars['page'] === 1) { // If page is 1, set FakeHeaders display to none
                    document.getElementById("stopFooter").style.display = 'none';
                }
if (vars['page'] === 2) { // If page is 1, set FakeHeaders display to none
                    document.getElementById("stopFooter").style.display = 'none';
                }
if (vars['page'] === 3) { // If page is 1, set FakeHeaders display to none
                    document.getElementById("stopFooter").style.display = 'none';
                }
            }
        }
    </script>
</head>

<body>
<div onload="subst()">
        <footer class="footer" id="stopFooter">
        <p>I am footer</p>
        <div class="line"></div>
        <p>Hello</p>
        </footer>
    </div></body>

</html>

And it's not working. Does wkhtmltodpf library give us some functions for that?

Upvotes: 0

Views: 1203

Answers (1)

TimonNetherlands
TimonNetherlands

Reputation: 1053

Remove onload="subst()" from the div.

Then change your function into a self invoking function by changing its opening and closing lines:

function subst() { becomes (function () { and the last (closing) } becomes })();

Don't forget to remove the script from the head. Place it into the body below your div. Otherwise the script will run before the HTML get's loaded without having any effect.

Upvotes: 1

Related Questions