简体   繁体   English

如何在不将边距设置为 0 的情况下从 window.print() 中删除页眉和页脚?

[英]How to remove header and footer from window.print() without set margin to 0?

I have an HTML code with a Table that I want to print using window.print().我有一个带有表格的 HTML 代码,我想使用 window.print() 进行打印。 I need to remove the headers and footers (The ones that the browsers add automatically, I mean, URL, number of page, etc.) on every page, but I need to keep the margin with an specific size.我需要删除每个页面上的页眉和页脚(浏览器自动添加的页眉和页脚,我的意思是 URL、页数等),但我需要将边距保持在特定大小。 This is my CSS:这是我的 CSS:

@page { 
    size: auto;
    margin: 5cm 0 5cm 0;
}
body {
    margin:0;
    padding:0;
}

table { page-break-inside:auto }
tr    { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }

I need to keep these 5cm because the company I'm working for is using previously letterheaded sheets.我需要保留这些 5 厘米,因为我工作的公司正在使用以前带有信头的纸张。 How can I remove these headers and footers without modifying my margin?如何在不修改边距的情况下删除这些页眉和页脚?

Just use the @media print query within your css.只需在您的 css 中使用@media print查询。 For example:例如:

@media print {
  thead, tfoot {  
    display: none !important
  }
}

I would wish it could be that easy.我希望它可以那么容易。 I mean, the automatic headers and footers that the browser is creating, that contains the URL, the number of page and other useless (In my situation) data.我的意思是,浏览器正在创建的自动页眉和页脚,其中包含 URL、页数和其他无用(在我的情况下)数据。

Ok, now, I got it.好的,现在,我明白了。 You can try this (should work on Chrome):你可以试试这个(应该适用于 Chrome):

@media print {
  @page {
    size: auto;
    margin: 0mm;
  }

  /* in case @page {margin: 5cm 0 5cm 0;} doesn't work */
  body {
    padding-top: 5cm !important;
    padding-bottom: 5cm !important;
  }
}

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

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