Laravel wkhtmltopdf - text in the bottom of the last page

2.2k Views Asked by At

I'm generating an invoice PDF using laravel snappy wkhtmltopdf and I'm tring to add some text in the bottom of the last page, now i already have a footer-html with the number of the page.

I tried to show the content only in the last page with this way:

<!DOCTYPE html>
<div id="footer_text">
     {!! nl2br($footer_text) !!}
</div>
<div class="pagination"><span id='page'></span> of <span id='topage'></span></div>
<script> 

    function remove(id) {
        var elem = document.getElementById(id);
        return elem.parentNode.removeChild(elem);
    }

    var vars={};
    var x=window.location.search.substring(1).split('&');
    for (var i in x) {
        var z=x[i].split('=',2);
        vars[z[0]] = unescape(z[1]);
    }

    document.getElementById('page').innerHTML = vars.page; 
    document.getElementById('topage').innerHTML = vars.topage; 

    if( vars.page != vars.topage && vars.topage > 1){
        document.getElementById('footer_text').innerHTML = '';
        remove('footer_text');
    }

    if(vars.topage == 1){
        document.getElementById('pages').innerHTML = '';
    }

</script> 

and it does show me the text only in the last page BUT in the previous pages I have a big white space, here is a screenshot:

page number 1: enter image description here

page number 2: enter image description here

I feel like i tried everything, please help me

3

There are 3 best solutions below

0
On

There is no issue with your script it might be some style issue. As you are removing footer_text in all previous pages and showing only on last page and this is somehow creating too much space. Check your CSS there must be margin-bottom or padding-bottom which is creating too much space. Enjoy!

0
On

Late to the party but looks like the ID footer_text will be set multiple times and ID's should be unique so I guess it would have worked if you used a class instead and getElementsByClassName

0
On

The footer height can't be dynamic on a different page when using Wkhtmltopdf. It's always with a static height. If this is your footer.html you have to add to your style:

body {height: 70px /for example/; position: relative;}

so you can align bottom (with position:absolute; bottom:0;) you #footer_text and content. But still, have some white space on all prev pages. In the PDF generators, the footer content area is independent of the body content.