简体   繁体   English

如何打印 header 和 HTML 页面的页脚

[英]How to print the header and footer of the HTML page

I tried to add the header and footer in each page for the HTML print.我尝试在 HTML 打印的每一页中添加 header 和页脚。 I write the below code我写下面的代码

@page {
  @top-left {
    content: "this is the page header";
  }
}

@page: right {
  @bottom-right {
    content: counter(page) "page";
  }
}

It is not working.它不工作。 I think the @bottom-right, @bottom-left etc features are removed in recent CSS update.我认为在最近的 CSS 更新中删除了@bottom-right、@bottom-left 等功能。 These elements not showing anything.这些元素没有显示任何东西。 I already tried the following code我已经尝试过以下代码

<style>
@media print {
  .page-header {
    position: fixed;
    top: 0;
    right: 0;
  }

  .page-footer {
    position: fixed;
    bottom: 0;
    right: 0;
  }
}
</style>
<section class='print-me'>
   <div class='row'>
      <div col='6'>..... dynamically generated text, graphs and images about 30 pages long which are difficult to handle manually ....</div>
   </div>
</section>

 <!-- I need this page header and footer in every page -->
 <h5 class="page-header">..... copyright and logo ...../h5>
 <h5 class="page-footer">..... page number: .....</h5>
 <!-- I want to generate page number dynamically -->

Since I have around 30 pages of HTML content having very long paragraphs which are generated dynamically, if I tried the second method, It will overlap with header and footer.由于我有大约 30 页 HTML 内容具有非常长的动态生成的段落,如果我尝试第二种方法,它将与 header 和页脚重叠。

edit : I found this article on medium .编辑我在 medium 上找到了这篇文章 This helps me to generate the header and footer somehow.这有助于我以某种方式生成 header 和页脚。 But this technique also breaks my tables inside the report.但是这种技术也破坏了我在报告中的表格。 There is also missing the page number generation part?也缺少页码生成部分?

Here's some tricks pulled from a project I've not yet published for generating Resumes...这是从我尚未发布的用于生成简历的项目中提取的一些技巧...

/* Thanks be to: https://stackoverflow.com/questions/19646835/print-repeating-page-headers-in-chrome/25880258#25880258
   And may allow for repeated headers only in print preview
 */
div[class*="resume-section-experience-"] {
  display: table;
  width: 100%
}
div[class*="resume-section-experience-"] > div {
  display: table-cell;
  overflow: hidden;
}

/* Add further section repetition hiding here */
.resume-section-experience-professional ~ .resume-section-experience-professional > div:before {
  content: "";
  display: block;
  margin-bottom: -3em; // inverse of header height
}
.resume-section-experience-volunteer ~ .resume-section-experience-volunteer > div:before {
  content: "";
  display: block;
  margin-bottom: -3em; /* inverse of header height */
}

.resume-headings {
  height: 3em;
}
div[class*="resume-section-experience-"] > div > div {
  display: inline-block;
  width: 100%;
  vertical-align: top;
}


/**
 * Attempt to keep section headings from displaying at the bottom of pages
 * and discourage page brakes within various content blocks.
 */
@media print {
  h2, h3, h4, ul, .professional-experience {
    page-break-after: avoid;
  }

  pre, blockquote, ul, div[class*="-training"], div[class*="-requirements"], div[class*="-experience"], div[class*="-skills"], div[class*="resume-section-experience-"] {
    page-break-inside: avoid;
  }
}

... you'll have to adjust element names/classes that are targeted, and perhaps fiddle with spacing between paragraphs, but the above code is what functions similar requirements. ...您将不得不调整目标元素名称/类,并且可能会调整段落之间的间距,但上面的代码是具有类似要求的功能。

All that said, if you know that paragraphs are going to be broken between pages at some-point, it may instead be better to shrink the available height of that container such that there is space for fixed positioned headers and footers.综上所述,如果您知道页面之间的段落会在某些时候被打断,那么缩小该容器的可用高度可能会更好,以便为固定定位的页眉和页脚留出空间。

If ya edit your question to include some of the markup (and notify me), I'll try to swing through again with edits to this answer that may better address your specific use cases.如果您编辑您的问题以包含一些标记(并通知我),我将尝试通过 swing 再次编辑此答案,以更好地解决您的特定用例。

One additional note on the above CSS;关于上述 CSS 的附加说明; for my use-cases I found that repeating header/footer data and then selectively hiding'em when not at the top/bottom of the page was easier to format between web and print views.对于我的用例,我发现重复页眉/页脚数据,然后在不在页面顶部/底部时选择性地隐藏它们更容易在 web 和打印视图之间进行格式化。


@dhiraj... The main problem is how to generate page number @dhiraj ...主要问题是如何生成页码

If page numbers be the main concern then it should be possible to utilize display: table , display: table-footer-group , and CSS's counter tricks...如果页码是主要关注点,那么应该可以利用display: tabledisplay: table-footer-groupCSS 的反技巧......

print.css

/**
 * print modifications **must** be within a style sheet
 */
@media print {
  /*
   * Note, forced page breaks are only for testing when content is insufficient
   */
  .page_content__print_break {
    page-break-after: always;
    break-after: always;
  }

  footer.footer_content {
    visibility: show;
    opacity: 1;
    filter: alpha(opacity=100);

    position: fixed;
    bottom: 0;
  }

  footer.footer_content::after {
    counter-increment: page_number;
    content: "Page:" counter(page_number);
  }
}

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Table based CSS footer example</title>

    <style media="screen">
      footer.footer_content,
      .page_content__print_break {
        visibility: hidden;
        opacity: 0;
        filter: alpha(opacity=0);
      }
    </style>

    <link rel="stylesheet" href="print.css">
  </head>


  <body>
    <section class="page_content">
      <div class="page_content__row">
        <p>This is where page content should be placed</p>
        <p>
          Thanks be to:
           <a href="https://stackoverflow.com/questions/20050939">
             Print page numbers on pages when printing html
          </a>
        </p>
      </div>

      <!--
        Note, following element is only for this example and should
        be removed for the use-case of this posted quesiton.
      -->
      <div class="page_content__print_break"></div>

      <div class="page_content__row">
        <p>
          More example content to perhaps force a second page to be printed...
        </p>

        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
          eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
          minim veniam, quis nostrud exercitation ullamco laboris nisi ut
          aliquip ex ea commodo consequat. Duis aute irure dolor in
          reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
          pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
          culpa qui officia deserunt mollit anim id est laborum.
        </p>
      </div>

    </section>


    <footer class="footer_content" role="contentinfo"></footer>
  </body>
</html>

Side note, upon reviewing the internally linked to Q&A above I found that @page that you're using within the current question may not be feature fully supported by CSS, to get more fanciness requires PagedJS旁注,在查看与上面问答的内部链接后,我发现您在当前问题中使用的@page可能不完全支持 CSS 的功能,以获得更多花哨需要PagedJS

And perhaps check the official CSS documentation on supported @page features.或许还可以查看官方 CSS 文档,了解支持的@page功能。

... however, after some testing it seems as though browser support has changed in recent years; ...然而,经过一些测试,似乎浏览器支持近年来发生了变化; both Firefox and Chrome seem to not do counter stuff when printing, and page-break-* has changed to break-* though that property now seems to be ignored by both browsers:-| Firefox 和 Chrome 在打印时似乎都没有做counter的事情,并且page-break-*已更改为break-*尽管现在两个浏览器似乎都忽略了该属性:-|

Edit;编辑; the comment by @contm stated that linking style sheets vs. style within head element may produce different results X-\ @contm的评论指出,在head元素中链接样式表与样式可能会产生不同的结果 X-\

In fact after reviewing Mozilla's developer documentation on break-after compatibility chart, this feature is supported by Internet Exporter事实上,在查看了 Mozilla 开发人员关于break-after兼容性图表的文档后,Internet Exporter 支持此功能

If I find anything that resolves these issues I'll be certain to pop by again with another edit to this answer.如果我找到任何可以解决这些问题的方法,我一定会再次对此答案进行编辑。


Updates;更新;

According to CSS spec.根据 CSS 规范。 for @page it's currently a WIP (draft or Work In Progress) so as of 2020 no vendors can be expected to implement.对于@page ,它目前是 WIP(草案或正在进行中的工作),因此到 2020 年,预计没有供应商会实施。

Footers that are fixed instead of repeated are only calculated once and after all increments have finished; fixed而不是重复的页脚只计算一次,并且在所有增量完成之后; an example that'll report Page: 2 on every page because there are two page_content__row s ...将报告Page: 2在每个页面上的示例,因为有两个page_content__row s ...

print.css

@media print {
  :root {
    counter-reset: page_count;
  }

  .page_content__row {
    counter-increment: page_count;
  }

  .page_content__print_break {
    page-break-after: always;
    break-after: always;
  }

  footer.footer_content {
    visibility: show;
    opacity: 1;
    filter: alpha(opacity=100);

    position: fixed;
    bottom: 0;
  }

  footer.footer_content::after {
    content: "Page: " counter(page_count);
  }
}

... aside from third-party libraries the only other option that I can think of for customizing footer information is calculating page breaks at the time that your content is dynamically generated, and then injecting footer element(s) where needed. ...除了第三方库之外,我能想到的用于自定义页脚信息的唯一其他选项是在动态生成内容时计算分页符,然后在需要的地方注入页脚元素。 This of course has the huge draw-back in that client font/zoom settings will either mess-up footer customizations or have to be ignored, which if ya go that route it may be simpler to instead generate the PDF document server-side and provide a download link.这当然有一个巨大的缺点,即客户端字体/缩放设置要么弄乱页脚自定义,要么必须被忽略,如果你是 go 这条路线,它可能更简单,而不是在服务器端生成 PDF 文档并提供下载链接。

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

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