1

We are using chromium playwright for converting html to pdf where header footer watermark are being set on pdf. But header need to be added on all pages except on page 1. Following is the snipped from method for setting headertemplate

// Header
pdfOptions.setHeaderTemplate(
        header.map(h ->
                "<div style='font-size:12px; padding-left:40px; text-align:left; width:100%;'>" +
                        "<b>" + h + "</b>" +
                        "<script>" +
                        "if (parseInt(document.querySelector('.pageNumber').textContent) === 1) {" +
                        "  document.currentScript.parentElement.style.display='none';" +
                        "}" +
                        "</script>" +
                        "</div>"
        ).orElse("<div></div>")
);
// Footer
pdfOptions.setFooterTemplate(
        footer.map(f ->
                "<div style='width:100%; font-size:10px; " +
                        "position: relative; " +
                        "display: flex; justify-content: flex-end; " +
                        "padding-right: 30px; " +
                        "padding-top: 10px; " +
                        "box-sizing: border-box;'>" +
                        f + " | Page&nbsp;<span class='pageNumber'></span>&nbsp;of&nbsp;<span class='totalPages'></span>" +
                        "</div>"
        ).orElse(
                "<div style='width:100%; font-size:10px; " +
                        "position: relative; " +
                        "display: flex; justify-content: flex-end; " +
                        "padding-right: 30px; " +
                        "padding-top: 10px; " +
                        "box-sizing: border-box;'>" +
                        "Page&nbsp;<span class='pageNumber'></span>&nbsp;of&nbsp;<span class='totalPages'></span>" +
                        "</div>"
        )
);
2
  • Two comments: (1) Why is this question tagged with java? There is no Java here. (2) What else have you tried? For example, have you tried the CSS @page :first style rule? (Not sure how well supported it is in different browsers and versions). Commented Sep 25 at 16:58
  • Take a look to stackoverflow.com/questions/41038989/… Commented Sep 26 at 8:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.