【问题标题】:CSS @page not working for update header and footer of print html pageCSS @page 不适用于更新打印 html 页面的页眉和页脚
【发布时间】:2020-01-02 17:44:05
【问题描述】:

我尝试了以下代码。使用 CSS 中的打印媒体更新打印页面内页眉和页脚的代码的目的

body {counter-reset: chapter;}
div.chapter {counter-increment: chapter;}
@page {
  margin: 10%;
  @top-center { content: "Chapter" counter(chapter) }
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./style.css"/ media="print">
</head>
<body>
<header></header>
<div id="pageHeader">
    <p>This is the Header shown on first page.</p>
</div>
<div id="pageFooter">
    <p>This is the footer shown on last page.</p>
</div>
<section class="page"> 

<h1>Introduction</h1>
<p>The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:</p>

<p>
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:

</p>
<p>The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:
The margins can be set as displayed in the examples above, or can be set for each side individually, as shown in the code snippet below, where the top and bottom margins are set to 2cm, and the left and right margins are set to 3cm:</p>


 </section>


 </body>
</html>

以下网址是我尝试过代码的地方 https://www.quackit.com/css/at-rules/css_bottom-center_at-rule.cfm https://www.w3.org/TR/css-page-3/#cascading-and-page-context

但是当我打印它时,它会应用 @page 规则的边距,但不会从其他定义规则更新页眉和页脚中的内容。 我附上了下面的图片,它显示了用于更改特定代码区域的规则

但是当我打印除@page(设置边距正确)之外的其他@rules 时不起作用

我想使用下面的 css @rule 更新打印页面中的突出显示区域

  • @bottom-center
  • @bottom-left-corner
  • @右下角

【问题讨论】:

标签: html css printing media


【解决方案1】:

根据屏幕截图页边距中的日期和 URL,您似乎正在使用桌面网络浏览器的“打印”功能。不幸的是none of them support CSS page-margin boxes (such as @top-center) at the moment

如果您想自己制作 PDF 文件或纸质副本,请考虑使用专用于打印渲染的工具,例如:

  • WeasyPrint(免责声明:我已经研究过这个)
  • Prince
  • (还有其他人,但我也不认识)

如果你正在制作一个网站,并且想控制其他人的网络浏览器如何打印它,恐怕你就不走运了。

【讨论】:

    【解决方案2】:

    我所有的@page css 通过 Prince 命令工作,它使用我定义的 css 为我创建 pdf

    这意味着我们必须使用@media print 来定义打印css

    关于打印页面中我的问题中突出显示的页眉和页脚,它们不受 @page 控制,@page 将创建包含所有定义 css 的 pdf

    我们不仅有prince工具,还有很多其他的通过命令行或gui将html转成pdf,命令之类的

    prince path/to/book.html --style path/to/book.css --output book.pdf
    

    它为我创建的 pdf

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2013-09-18
      • 2016-03-05
      • 2012-04-15
      • 2020-02-06
      相关资源
      最近更新 更多