【问题标题】:Weasyprint and CSS: header, footer, pagebreak and positioningWeasyprint 和 CSS:页眉、页脚、分页符和定位
【发布时间】:2020-09-25 08:22:09
【问题描述】:

我正在用 html 构建一个发票报告模板,并使用 Wea​​syprint 将其生成为 PDF(最终生成为 docx)

我遇到的问题是不仅无法分页,而且无法正确生成正在运行的页眉和页脚,而正文内容不会重叠并将我的数据转换为 zalgo 文本。

我的报告模板有这种简单的格式:

+==========================+
+ Header                   + 
+==========================+
+ Body                     +
+==========================+
+ Footer                   +
+==========================+

页眉和页脚都会或多或少地出现在页面上。页眉包含一个页面计数器,而页脚将在文本框中显示一个值仅在最后一页

我的页眉和页脚都被引用到单独的 HTML 模板以实现多功能性,使用 include 关键字来包含它们。由于这是发票的模板,因此标题更类似于信头。

主要内容将在正文中。如果内容过多,则会中断并继续到下一页。

对于所有 3 个部分,我使用表格进行格式化,主要是为了保持数据对齐。

这是我的主要 HTML 正文的示例:

<!DOCTYPE html>
<html>
<head>
    <style type="text/css" media="all">
        @page {
                size: A4 portrait; /* can use also 'landscape' for orientation */
                margin: 1cm;

                @top-left{
                    content: element(header);
                }

                @bottom-left{
                    content: element(footer);
                }

        }

        header, footer, .body_content{
           font-size: 12px;
           /* color: #000; */
           font-family: Arial;
           width: 100%;
           position: relative;
        }


        header {
            /*position: fixed;*/
            /*position: running(header); */
            /*display: block; */
        }

        footer {
            position: fixed;
            /*position: running(footer);*/
            /*position: absolute;*/
            bottom: 0;
            /*display: block;*/
        }

        .body_content {
            position: relative;
            page-break-inside: auto;
            height: 320pt;
            /*overflow: hidden;*/
        }
    </style>

</head>

<body>
    <header>
    {% include 'sampleTemplate_header.html' %}
    </header>
    <div >
        <table class="body_content">
            <tbody >
                <tr style="padding-top:5px;" >
                    <td style="width:60%;" >
                    </td>
                    <td style="width:10%;" >
                    </td>
                    <td style="width:15%;" >
                    </td>
                    <td style="width:15%;" >
                    </td>
                </tr>
                <tr>
                </tr>
                <tr >
                    <td style="width:60%;" >
                    </td>
                    <td style="width:10%;" >
                    </td>
                    <td style="width:15%;" align="right" >
                    </td>
                    <td style="width:15%;" align="center" >
                    </td>
                </tr>


                    <tr >
                        <td style="width:60%;" id="testCell" >
                        </td>
                        <td style="width:10%;" >
                        </td>
                        <td style="width:15%;" >
                        </td>
                        <td style="width:15%;" >
                        </td>
                    </tr>

            </tbody>
        </table>

    </div>  

    <footer>
    {% include 'sampleTemplate_footer.html' %}
    </footer>
</body>

</html>

由于我对布局进行了试验,CSS 部分有很多注释代码,但尽管我更改了很多,但我似乎无法获得我需要的布局。

我最普遍的问题之一是正文内容的文本与页眉或页脚重叠。后者甚至会发生,尽管强制 page-break-after

【问题讨论】:

  • 你有没有让这个工作,特别是仅在最后一页部分?我正在为同样的事情苦苦挣扎,目前似乎不可能。

标签: python html css templates weasyprint


【解决方案1】:

我让它与运行元素一起工作(我在这里读到它:https://www.w3.org/TR/css-gcpm-3/#running-elements)。

如果您将页脚放在主要内容之前,那么它将显示在每个页面上。运行元素显然将元素从主流移动到边距,所以我猜如果它不在页面中但它不能将它移动到边距。

为了使页眉/页脚停止与内容重叠,我不得不使用@pagemargin 值。由于运行元素会将元素移动到边距中,因此使边距更大会为其提供更多空间。在下面的示例中,如果您减小顶部(或底部)边距的值,则页眉(或页脚)将重叠。

有时我必须设置页眉/页脚 height 值才能使其正确定位在边距中,但在此示例中我不必这样做。

<!DOCTYPE html>
<html>
<head>
    <style type="text/css" media="all">
        @page {
                size: A4 portrait; /* can use also 'landscape' for orientation */
                margin: 100px 1cm 150px 1cm;

                @top-left{
                    content: element(header);
                }

                @bottom-left{
                    content: element(footer);
                }

        }


        header {
            position: running(header); 
            /*height: 100px;*/
        }

        footer {
            position: running(footer);
            /*height: 150px;*/
        }
    </style>

</head>

<body>
    <header>
    multiline<br>header<br>lots<br>of<br>lines<br>here<br>
    </header>

    <footer>
    multiline<br>footer<br>lots<br>of<br>lines<br>here
    </footer>
    <div >
        stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff<br>stuff

    </div>  
</body>

</html>

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 2017-09-25
    • 2017-02-17
    • 1970-01-01
    • 2014-11-05
    • 2016-11-30
    • 1970-01-01
    • 2013-07-14
    • 2014-07-07
    相关资源
    最近更新 更多