简体   繁体   English

在最后一页的最底部打印表格页脚

[英]Print table footer at the very bottom on last page

I am using a table to create a footer on every page (works in Firefox, that's enough).我正在使用一个表格在每个页面上创建一个页脚(在 Firefox 中工作,这就足够了)。

A JS Fiddle: https://jsfiddle.net/j9k2xzze/一个 JS 小提琴: 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 扩展在 Firefox 中不起作用,或者我做错了:

@page:last {
  #footer {
    position: absolute;
    bottom: 0;
  }
}

If you're only supporting Firefox, this is actually really easy. 如果您只支持Firefox,这实际上非常简单。 (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). (跳到编辑,看看一种技术也可以在IE中运行但功能不太多。当我发布这个答案时,Chrome和其他webkit浏览器不支持重复页脚,所以我没有测试它们)。

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). 边距与其他形式的空白区域(例如填充和<br>标记)不同,因为它们在超出页面边界时会被取消( 请参阅规范 ,第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. Firefox和IE都会删除边距,但Firefox仍然会在页面底部生成一个页脚,就好像发生了分页一样。 (IE, on the other hand, behaves as if the margin was never there, which is why this approach doesn't work in that browser.) (IE,另一方面,表现就好像边距从未出现过,这就是为什么这种方法在该浏览器中不起作用的原因。)

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. 您可以将边距放在伪元素上以保持HTML整洁,并且可以使用@media print来阻止它显示在屏幕上。

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. 要在Firefox中查看它,请打开此jsfiddle ,右键单击输出,选择“此帧”>“仅显示此帧”,然后执行打印预览。

 @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. 还有另一种选择适用于Firefox和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. 您所要做的就是将页脚放在单独的<div>并将其固定到页面底部,然后使用重复的<tfoot>作为间隔符。 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. 要在Firefox中查看它,请打开此jsfiddle ,右键单击输出,选择“此帧”>“仅显示此帧”,然后执行打印预览。 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". 在IE中,单击输出框,按CTRL + A,执行打印预览,并将“在屏幕上显示为”更改为“在屏幕上选择”。

 @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. 此外,由于垫片的高度取决于页脚的高度,如果页脚高度发生变化,则必须调整它。

There is no way to have footer only on last page if you use absolute or fixed position. 如果您使用绝对或固定位置,则无法在最后一页上设置页脚。 If you want footer on the bottom only if page in not long enough, use sticky footer technique thats doesn't require absolute/fixed position. 如果您只想在页面不够长的情况下在底部使用页脚,请使用不需要绝对/固定位置的粘性页脚技术。 Something like this 像这样的东西

html, body {
  height: 100%;
  margin: 0;
}
.wrapper {
  min-height: 100%;
  margin-bottom: -50px;
}
.footer,
.push {
  height: 50px;
}

If you really need footer at the bottom of each page I would recommend to generate pdf first and print it. 如果你真的需要每页底部的页脚,我建议先生成pdf并打印出来。 If you need to print complex stuff you will end up generating pdfs first anyway, print css is very limited. 如果你需要打印复杂的东西,你最终会最终生成pdf,print css非常有限。

This is a fun question, never encountered/needed this, so I learned something new too. 这是一个有趣的问题,从未遇到/需要这个,所以我也学到了新的东西。 But here's my working solution in short (you were actually pretty close): 但这是我的工作解决方案(你实际上非常接近):

With a #wrapper { position: relative } , the #header , #footer and #content { position: absolute } and additional location and height rules your CSS ( HTML unchanged) you are fully in control of the layout. 随着#wrapper { position: relative }#header#footer#content { position: absolute }和额外的位置和高度掌管着你的CSS(HTML不变),你完全在布局的控制。 Your code could look something like this: 您的代码可能如下所示:

Snippet below, jsFiddle: fiddle and fiddle output frame 下面的片段,jsFiddle: 小提琴小提琴输出框架

 html,body { height: 100%; width: 100%; overflow: hidden } #wrapper { position: relative; height: 95%; width: 100%; } #wrapper #header { position: absolute; top: 0; height: 100px; } #wrapper #content { position: absolute; top: 100px; } #wrapper #footer { position: absolute; bottom: 0; } @media print { h1 { page-break-before: always; } @page { size: A4; margin: 0; } } 
 <table id="wrapper"> <thead> <tr> <td id="header">HEADER TEXT</td> </tr> </thead> <tfoot> <tr> <td colspan="0" id="footer"> <img src="https://unsplash.it/200?image=031" /> </td> </tr> </tfoot> <tbody> <tr> <td id="content"> Welcome <h1>NEXT PAGE</h1> just one line </td> </tr> </tbody> </table> 

Hi I just added the following media query to your CSS. 嗨,我刚刚将以下媒体查询添加到您的CSS。 And it WORKS 起作用了

@media print {
 #footer {
    position: absolute;
    bottom: 0;
  }
}

Check the following fiddle 检查以下小提琴

https://jsfiddle.net/jjsqgaza/ https://jsfiddle.net/jjsqgaza/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM