Different margins of first and last page in wkhtmltopdf
Asked Answered
D

0

7

I have problem with my wkhtmltopdf. I need to generate document from HTML, which contains header on the top of first page and footer on bottom of last page.

In my opinion the best solution was to divide my HTML for 3 separate files: "header.html", "content.html" and "footer.html". It is possible with wkhtmltopdf. I managed with issue to set this header on the first page only and footer on last page only using this solution:

wicked_pdf: Is it possible to have the header only show on the first page & the footer only on the last?

The problem is that my header and footer are big (5cm height). That's why I need to reserve margin-top and margin-bottom in wkhtmltopdf with this value to have my header and footer "fitted" in that margins. This causes that I have 5cm margin-top and margin-bottom on every page of my document.

I used wkhtmltopdf command:

wkhtmltopdf --header-html 'my header html' --footer-html 'my footer html' --margin-right '0' --margin-left '0' --margin-top '50' --margin-bottom '50' --print-media-type 'my content html' test.pdf

To set header only on the first page I have this JavaScript:

function headerCheck() {
    var x=document.location.search.substring(1).split('&');
    for (var i in x) {
      if(x[i] == "page=1")
        document.getElementsByClassName("headerContent")[0].style.display = "block";
    }
  };

To set footer only on the last page I have this Javascript:

function footerCheck() {
       var x=document.location.search.substring(1).split('&');
       var currentPage = 1;
       for (var i in x) {
         var z=x[i].split('=',2);
         if(z[0] == "page") {
            currentPage = decodeURI(z[1]);
         }
         if(z[0] == "topage" && currentPage == decodeURI(z[1])) {
            document.getElementsByClassName("footerContent")[0].style.display = "block";
         }
        }
     };

After doing the staff above I have my header only on the first page and footer only on the last page, but in every page there is huge 5cm margins. I need to have them smaller on pages with content (exactly like in the picture):

https://ibb.co/hKzTJrm

Thank you in advance for your help :)

Digitalin answered 10/9, 2019 at 8:57 Comment(2)
how you fix this ?Sentient
I would like to know also, how to fix it, there is one possible solution, to add header content to page content and remove header completely, since it is only on the first page, it makes no differenceInterstellar

© 2022 - 2024 — McMap. All rights reserved.