是否有可能在每个打印页面上打印带有自定义页眉和页脚的HTML页面?

我想在每个打印页面的顶部和底部添加“UNCLASSIFIED”字样,字体为红色,Arial,大小为16pt,无论内容如何。

为了澄清,如果文档打印到5页上,那么每页都应该有自定义页眉和页脚。

有人知道使用HTML/CSS是否可行吗?


当前回答

神奇的解决方案是把所有东西都放在一个表中。

标题:用于重复的标题。 Tfoot:重复的页脚。 Tbody:内容。

做一个单独的tr td,把所有东西都放在div里

代码:

<table class="report-container">
   <thead class="report-header">
     <tr>
        <th class="report-header-cell">
           <div class="header-info">
            ...
           </div>
         </th>
      </tr>
    </thead>
    <tfoot class="report-footer">
      <tr>
         <td class="report-footer-cell">
           <div class="footer-info">
           ...
           </div>
          </td>
      </tr>
    </tfoot>
    <tbody class="report-content">
      <tr>
         <td class="report-content-cell">
            <div class="main">
            ...
            </div>
          </td>
       </tr>
     </tbody>
</table>

table.report-container {
    page-break-after:always;
}
thead.report-header {
    display:table-header-group;
}
tfoot.report-footer {
    display:table-footer-group;
} 

额外:防止多页重叠。如:

<div class="main">
    <div class="article">
        ...
  </div>
    <div class="article">
        ...
  </div>
    <div class="article">
        ...
  </div>
  ...
  ...
  ...
</div>

这将导致溢出,将使内容在分页符内与页眉重叠。

所以>>使用:page-break-inside: avoid !与这类文章。

table.report-container div.article {
    page-break-inside: avoid;
}

其他回答

从这个问题开始——将以下样式添加到仅打印的样式表中。此解决方案将在IE和Firefox中工作,但不适用于Chrome(截至版本21):

#header {
  display: table-header-group;
}

#main {
  display: table-row-group;
}

#footer {
  display: table-footer-group;
}

穆罕默德·穆萨维的评论是最好的答案,所以这里出现了一个实际的答案:

在每页的顶部和底部自动重复标题/页脚。然而,tfoot并不粘到最后一页的底部。

位置:固定打印将在每页重复,页脚将粘在所有页的底部,包括最后一页-但是,它不会为其内容创造空间。

把它们:

HTML:

<header>(repeated header)</header>

<table class=paging><thead><tr><td>&nbsp;</td></tr></thead><tbody><tr><td>

(content goes here)

</td></tr></tbody><tfoot><tr><td>&nbsp;</td></tr></tfoot></table>

<footer>(repeated footer)</footer>

CSS:

@page {
    size: letter;
    margin: .5in;
}

@media print {
    table.paging thead td, table.paging tfoot td {
        height: .5in;
    }
}

header, footer {
    width: 100%; height: .5in;
}

header {
    position: absolute;
    top: 0;
}

@media print {
    header, footer {
        position: fixed;
    }
    
    footer {
        bottom: 0;
    }
}

你可以在这里添加很多细节,但我故意将其削减到最少,以获得一个干净的渲染页眉和页脚,在屏幕上出现一次,在每个打印页面的顶部和底部出现一次。

https://medium.com/@Idan_Co/the-ultimate-print-html-template-with-header-footer-568f415f6d2a

多年来,我一直在寻找一个解决方案,并找到了这篇关于如何在多个页面上打印一个页脚而不重叠页面内容的文章。

我的要求是IE8,到目前为止,我发现这不能在Chrome中工作。从2018年3月1日起,它也可以在Chrome浏览器中使用

下面的例子通过设置css样式来使用表格和tfoot元素:

tfoot {display: table-footer-group;}

我找到了一个解决这个问题的方法:

当你用“page”className分隔每一页时,你可以给“page”这个样式:

.page{
  height: 1150px;
  page-break-after: always;
  overflow: hidden;
}

检查打印页面的预览,确保所有内容都在页面内,否则移动内容,让所有内容看起来都很整齐。

根据我的工作经验,我想我的职位是:固定工作。

body { background: #eaeaed; -webkit-print-color-adjust: exact; } .my-footer { background: #2db34a; bottom: 0; left: 0; position: fixed; right: 0; } .my-header { background: red; top: 0; left: 0; position: fixed; right: 0; } <html> <head> <meta charset=utf-8 /> <title>Header & Footer</title> </head> <body> <div> <div class="my-header">Fixed Header</div> <div class="my-footer">Fixed Footer</div> <table> <thead> <tr> <th>TH 1</th> <th>TH 2</th> </tr> </thead> <tbody> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> <tr> <td>TD 1</td> <td>TD 2</td> </tr> </tbody> </table> </div> </body> </html>

在chrome中按Ctrl+P查看每页的页眉和页脚文本。 希望能有所帮助