Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print table footer at the very bottom on last page

Tags:

I am using a table to create a footer on every page (works in Firefox, that's enough).

A JS Fiddle: https://jsfiddle.net/j9k2xzze/

(right click on the output pane -> This Frame -> Open Frame in New Tab. Then Print Preview will function as normal)

<table id="wrapper">     <thead>     <tr>         <td id="header"></td>     </tr>     </thead>      <tfoot>     <tr>         <td colspan="0" id="footer">             <img src="footer.jpg"/>         </td>     </tr>     </tfoot>      <tbody>     <tr>         <td id="content">             <?php echo $html; ?>         </td>     </tr>     </tbody> </table> 

But on the very last page the table footer is displayed directly below the text. If the text is shorter than the last page, the footer sticks to it.

I like the footer to be at the very bottom on the last page. Unfortunately the @page extension does not work in firefox or I am doing it wrong:

@page:last {   #footer {     position: absolute;     bottom: 0;   } } 
like image 942
LeMike Avatar asked Oct 31 '15 06:10

LeMike


People also ask

How do I print the footer on the last page only in HTML?

Try print mode in chrome and you can see that the footer now appears on the last page only at the bottom of the page. position: fixed in print mode makes the footer appear on all the pages at the bottom of every page. Here is the full screen view of the fiddle result. fiddle.jshell.net/ur6d1h21/12/show.

How do I keep the footer span at the bottom of the page?

We can simply add margin-top:auto to the footer and it will be pushed to the bottom of the page regardless his height or the height of the content above.

How do I add a footer to the last page in HTML?

So if you put the footer div at the end of the HTML, it will only appear on the last page. It also works if you want a footer on each page, with a different content for the last page. You just have to define the footer div twice in the HTML.

How do I change the position of a footer in CSS?

position: fixed; bottom: 0 and this one. This solution is the best because the footer does not stick to the bottom, but stays at the end of the scrollable page. to build on your answer, you can look at it another way by setting the . footer {max-height: calc(100vh+40px); position: absolute} .


1 Answers

If you're only supporting Firefox, this is actually really easy. (Skip to the edit to see a technique that also works in IE but is less versatile. Chrome and the other webkit browsers didn't support repeating footers when I posted this answer, so I didn't test them).

All you have to do is add a large bottom margin at the end of your content. The exact size doesn't matter, but it must be large enough to be guaranteed to run past the end of the page. I recommend making it at least as large as the maximum paper size you think your users will use.

Don't worry, this won't add a blank page to the end of your document. Margins differ from other forms of white space (such as padding and <br> tags) in that they get cancelled out when they exceed the page boundary (see spec, section 13.3.3). Both Firefox and IE will delete the margin, but Firefox will still generate a footer at the bottom of the page as if a page break had occurred. (IE, on the other hand, behaves as if the margin was never there, which is why this approach doesn't work in that browser.)

You can put the margin on a pseudo-element to keep your HTML tidy, and you can use @media print to prevent it from showing on screen.

Here's the code. To see it work in Firefox, open this jsfiddle, right-click the output, select This Frame > Show Only This Frame, and do a print preview.

@media print {    #content:after {      display: block;      content: "";      margin-bottom: 594mm; /* must be larger than largest paper size you support */    }  }
<table>    <thead>      <tr>        <th>PAGE HEADER</th>      </tr>    </thead>    <tfoot>      <tr>        <td>PAGE FOOTER</td>      </tr>    </tfoot>    <tbody>      <tr>        <td id="content">          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content        </td>      </tr>    </tbody>  </table>

EDIT

There's another option that works in both Firefox and IE. All you have to do is put the footer in a separate <div> and fix it to the bottom of the page, and then use the repeating <tfoot> as a spacer. This approach does have some minor drawbacks, though (details below snippet).

Here's the code. To see it work in Firefox, open this jsfiddle, right-click the output, select This Frame > Show Only This Frame, and do a print preview. In IE, click in the output frame, hit CTRL+A, do a print preview, and change "As Laid Out On Screen" to "As Selected On Screen".

@media print {    #spacer {height: 2em;} /* height of footer + a little extra */    #footer {      position: fixed;      bottom: 0;    }  }
<table>    <thead>      <tr>        <th>PAGE HEADER</th>      </tr>    <thead>    <tfoot>      <tr>        <td id="spacer"></td>      </tr>    </tfoot>    <tbody>      <tr>        <td>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content<br>          content<br>content<br>content<br>content<br>content<br>content<br>content        </td>      </tr>    </tbody>  </table>    <div id="footer">    PAGE FOOTER  </div>

The main limitation of this method is that it puts an identical footer on every page in the print job, which means you can't have any pages with a different footer, or no footer. Also, since the height of the spacer depends on the height of the footer, you'll have to adjust it if the footer height ever changes.

like image 77
DoctorDestructo Avatar answered Sep 26 '22 03:09

DoctorDestructo