5

您好,我已经使用CSS3 页面媒体模块直接从浏览器将 html 内容打印到 PDF,但我没有让任何浏览器可以使用它。我的工作如下。

<!DOCTYPE html>
<html lang="en">
<head>
<style>
@media print {

body {
        margin: 0;
        padding: 0;
        background-color: #FAFAFA;
        font: 12pt "sans-serif";
        position: relative;
    }
    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
    }


        @page {
            size: letter;
            margin: 20mm 25mm;
        }
        @page {
            @top-center {
                content: element(pageHeader);
            }
        }

        @page  {
            @bottom-center {
                content: element(pageFooter);
            }
        }
        #pageHeader {
          position: running(pageHeader);
        }
        #pageFooter{
          position: running(pageFooter);
        }

    }
</style>
</head>
<body>
<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>

它不适用于我的任何浏览器。我已经在最新的 Chrome、FF、Opera、Safari 中对其进行了测试。它应该在每一页上显示页眉和页脚。页眉旨在显示在每个页面的顶部,而页脚则显示在每个页面的底部。任何人都可以有实际工作的 CSS3 页面媒体模块元素的工作示例。此外,我不是在寻找绝对位置或页眉和页脚的固定解决方案。我已经尝试过了,除了单页之外它不起作用。我已经为这个问题缩短了我的文本内容,但它可以被复制和过去以生成多页文档以查看没有页眉和页脚的多个页面。页眉和页脚 div 保持在顶部。

4

2 回答 2

3

目前,任何最终用户浏览器都不支持它,但在用于生成 PDF 的专用引擎 PrinceXML 中支持它。请参阅对边距框的 CSS3 支持

PrinceXML 是一个商业产品,但我使用它从 HTML 源创建小说的印刷版本。它支持打印相关功能的完整列表。(我与他们没有任何关系。)

于 2014-08-22T23:29:26.697 回答
1

可能是因为该规范来自 W3C 工作草案而不是实际实现,所以还没有浏览器实现这个提议的规范?事实上,它甚至还没有被提出——它正在作为“工作草案”进行讨论,这意味着它可以进行讨论,甚至还没有成为“编辑草案”。从那里开始,您可能还需要一段时间才能在浏览器中看到它。该工作草案也有一个相对较新的日期 - 2014 年 5 月 13 日 - 所以要注意在一段时间内实施任何该提案。也就是说,这看起来确实是一个方便的补充。

现在,如何在该标题位置使用 CSS 伪元素“之前”或“之后”以及标签?

这里很好地描述了工作草案和编辑草案 之间的区别:W3C 工作草案和编辑草案有什么区别?

于 2014-08-22T21:44:30.520 回答