有没有办法在每一页上打印网页页眉/页脚? [英] Is there a way to get a web page header/footer printed on every page?

查看:32
本文介绍了有没有办法在每一页上打印网页页眉/页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的研究,我想做的事情似乎是不可能的,但万一发生了变化,我想看看有没有人想出办法来做到这一点.

Based on my research, it seems that what I want to do is not possible, but in case something has changed, I wanted to check to see if anyone had come up with a way to do this.

我有一个 Web 应用程序,它根据用户在浏览器窗口中的选择生成要打印的报告.我有一个自定义页眉和页脚,当从浏览器打印报告时,应该在每个打印页面上重复.我需要的不是浏览器页眉和页脚,而是我生成的自定义页眉和页脚.另外,我不认为这是 CSS 和媒体类型的问题,但我不是 CSS 专家.我没有问题让页眉和页脚打印一次,但我无法让它们在每一页上打印.我已经读过,如果我使用表格重新创建我的报告页面,然后使用表格标题标签和 CSS,那至少可以在每个页面上获取标题.我还没有成功,但如果这是唯一的选择,我会再试一次.一位同事建议我在我的php中计算行数,并根据需要手动放出页眉和页脚.我想这是一个选项,但似乎应该有一种方法来做到这一点,而不是蛮力"!

I have a web app that generates reports for print based on user selections in the browser window. I have a custom header and footer that, when the report is printed from the browser, should be repeated on every printed page. It is not the browser header and footer I need, but rather the custom ones that I generate. Also, I don't think this is an issue of CSS and media types, but I am not a CSS expert. I have no issues getting the header and footer to print once, but I can't get them to print on each page. I have read that perhaps if I recreated my report pages using tables, and then used table head tags and CSS, that may work at least to get the header on each page. I have not had success with this yet, but I will try it again if it is the only option. A coworker suggested that I count lines in my php and manually put out the header and footer as required. I guess that is an option, but it just seems like there should be a way to do this that isn't so "brute force"!

另一个警告是我必须支持 IE 6,所以我怀疑我尝试过的一些 CSS 东西不支持.

The other caveat is that I have to support IE 6, so I suspect some of the CSS things I have tried are just not supported.

如果有人知道这样做的方法,那就太好了!如果没有,我将不得不重新考虑我的方法.

If anyone knows any way to do this, that would be great! If there isn't, I will have to rethink my approach.

提前致谢!

更新(2011 年 12 月 14 日)

我在这个问题上取得了相当大的进展,并使用答案中的一些信息,我确实制作了可用的报告,但从来没有像我想要的那样好或专业.页脚往往离页面底部不够近,我不得不做很多猜测工作和脆弱"的计算来决定插入分页符的文本有多大,我只能支持受限制的一组页面格式,以及对报告的任何更改都会导致一系列代码更改和更脆弱的计算.总有一个场景会破坏某些报告的某些部分.我们修改了要求,现在正在使用 TCPDF 生成 PDF 格式的报告.文档有点不透明,需要进行一些实验,但结果要好得多,现在报告应有尽有.我想对任何试图从浏览器生成 HTML 报告的人说,除非它们非常简单,否则请避免沮丧(正​​如其他人在此处告诉我的那样)并使用 PDF 或类似内容.

I made considerable progress with this issue, and using some of the info from the answers, I did produce reports that were usable, but never as nice or as professional as I wanted. Footers would tend to be not close enough to the bottom of the page, I had to do a lot of guess work and "brittle" calculations about how big text was going to be to decide on inserting page breaks, I could only support a restricted set of page formats, and any changes to the reports resulted in a cascade of code changes and yet more brittle calculations. There was always a scenario that broke some part of some report. We revisted the requirements, and are now generating reports as PDFs using TCPDF. The documentation is a bit opaque, and it takes some experimentation, but the results are far superior and now the reports appear as they should. I would say to anyone trying to do HTML reports from the browser, unless they are very simple, save yourself the frustration (as others told me here) and go with PDFs or something similar.

推荐答案

它可以用表格来完成——我知道我会因为建议使用表格进行布局而冒着投反对票的风险——但我们在这里谈论的是 IE6并不以其出色的 CSS 支持而闻名:-)

It can be done with tables -- and I know I'm going to risk a downvote by suggesting using tables for layout - but we are talking IE6 here which isn't known for its fantastic CSS support :-)

如果你设置一个CSS样式如下:

If you set a CSS style as follows:

thead { display: table-header-group; }
tfoot { display: table-footer-group; }

然后,当您创建 HTML 时,将您的正文呈现为:

Then when you create your HTML, render your body as:

<body>
<table>
   <thead><tr><td>Your header goes here</td></tr></thead>
   <tfoot><tr><td>Your footer goes here</td></tr></tfoot>
   <tbody>
     <tr><td>
     Page body in here -- as long as it needs to be
     </td></tr>
   </tbody>
</table>
</body>

是的,它不好(表格与 CSS),它并不理想,但是(对您来说很重要)它确实适用于 IE6.我无法对 Firefox 发表评论,因为我没有在那里测试过它,但它应该可以完成这项工作.这也将处理不同大小的页面、不同的字体大小等,因此它应该正常工作".

Yes it's not good (tables vs CSS), it's not ideal, but (importantly for you) it does work on IE6. I can't comment on Firefox as I've not tested it there, but it should do the job. This will also handle differnt sized pages, differing font sizes, etc. so it should "just work".

如果您希望页眉和页脚仅出现在印刷媒体上,请使用@media 参数来做正确的事情:

If you want the header and footer to appear on printed media only, then use the @media parameters to do the right thing:

@media print {
    thead { display: table-header-group; }
    tfoot { display: table-footer-group; }
}
@media screen {
    thead { display: none; }
    tfoot { display: none; }
}

注意
截至 2015 年 7 月,这仍仅适用于 Firefox 和 IE.基于 Webkit 的浏览器(参见 Chrome、Safari)在他们的问题跟踪器中存在长期存在的错误,如果有人强烈认为可以对其进行投票:

这个问题下面的评论告诉我这个问题现在已经在 Chrome 中解决了.我没有检查过自己:-)

The comments below this question tell me this is now resolved in Chrome. I haven't checked myself :-)

针对 Chrome 的原始错误(供参考)是:

The original bugs against Chrome (for reference) are:

这篇关于有没有办法在每一页上打印网页页眉/页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆