directory
directory

Reputation: 3167

wkhtmltopdf getting page count inside body html

In my application I generate pdf's with a separated header, footer and body. In some pdf's I have included a sidebar with a color, the problem I am facing is that I need to calculate the hight of the sidebar. I know the perfect height for two pages is 1100px height, now I want to include javascript as in my example to get the total pages so I can calculate a new height for the sidebar.

My code;

<script>  
      var pdfInfo = {};
      var x = document.location.search.substring(1).split('&');
      for (var i in x) { var z = x[i].split('=',2); pdfInfo[z[0]] = unescape(z[1]); }
      function getPdfInfo() {
        var page = pdfInfo.page || 1;
        var pageCount = pdfInfo.topage || 1;
        document.getElementById('pdfkit_page_current').textContent = page;
        document.getElementById('pdfkit_page_count').textContent   = pageCount;

        document.getElementsByClassName('wrapper').innerHTML(pageCount);
      }
    </script> 
</head>
<body onload="getPdfInfo()">

    <div class="wrapper">
    ...more html

But I don't get any pageCount results inside the wrapper. Is this even possible in a body html template or just inside the header/footer templtes? Thank you for the advice.

Upvotes: 2

Views: 1293

Answers (1)

Robin van Breukelen
Robin van Breukelen

Reputation: 517

It's not a really great solution but a workaround is to simply have your sidebar with a very large height. I use a sidebar with the following css:

    display: block;
    position:fixed;
    height:5000px;
    bottom:0;
    top:0;
    left:0;
    right:0;
    background-color: #feb900;
    width: 30px;

The result is a sidebar that spans the necessary pages without creating a new page. Also be aware that wkhtmltopdf will insert margins (10mm by default) you can change that by adding the following flags:

  -B 0 -T 0 -L 0 -R 0

Upvotes: 1

Related Questions