Django easy pdf,文档每页都有标题

Django easy pdf,文档每页都有标题,django,pdf,django-templates,Django,Pdf,Django Templates,我正在尝试导出PDF文件中的一些数据。我正在使用: Django 1.9.12 django easy pdf 0.1.0 Python 2.7 导出工作得很好(我的视图没有问题),但我正在努力将页眉添加到文档的每一页中 此时,我只能在第一页上渲染它。我对页脚没有这样的问题,它在每一页上都能正确呈现 我的模板如下: {% extends "easy_pdf/base.html" %} {% block extra_style %} <style type="text/css">

我正在尝试导出PDF文件中的一些数据。我正在使用:

  • Django 1.9.12
  • django easy pdf 0.1.0
  • Python 2.7
导出工作得很好(我的视图没有问题),但我正在努力将页眉添加到文档的每一页中

此时,我只能在第一页上渲染它。我对页脚没有这样的问题,它在每一页上都能正确呈现

我的模板如下:

{% extends "easy_pdf/base.html" %}

{% block extra_style %}
<style type="text/css">

    @page {
        size: landscape;
        margin-left: 1cm;
        margin-right: 1cm;
        margin-top: 1.5cm;
        margin-bottom: 2cm;

        @frame footer {
            -pdf-frame-content: page-footer;
            bottom: 0cm;
            margin-left: 1cm;
            margin-right: 1cm;
            height: 2cm;
        }
    }

</style>
{% endblock %}

{% block page_header %}
    {% include "document_head.html" %}
{% endblock %}

{% block content %}
    {% include "main_table.html" %}
{% endblock %}

{% block page_foot %}
    {% include "document_foot.html" %}
{% endblock %}
<!DOCTYPE html>
<html>
<head>

    {% block style_base %}
        {% block layout_style %}
            <style type="text/css">

            </style>
        {%endblock%}
        {% block extra_style %}{% endblock %}
    {% endblock %}

</head>
<body>

    <div id="page-header">
        {%block page_header%}
        {%endblock%}
    </div>

    <div>
        {%block content%}
        {%endblock%}
    </div>

    <div id="page-footer">
        {%block page_foot%}
        {%endblock%}
    </div>
</body>
{%extends“easy\u pdf/base.html”%}
{%block extra_style%}
@页面{
规模:景观;
左边距:1厘米;
右边距:1cm;
边缘顶部:1.5cm;
边缘底部:2cm;
@框架页脚{
-pdf框架内容:页脚;
底部:0厘米;
左边距:1厘米;
右边距:1cm;
高度:2厘米;
}
}
{%endblock%}
{%block page_header%}
{%include“document_head.html”%}
{%endblock%}
{%block content%}
{%include“main_table.html”%}
{%endblock%}
{%block page_foot%}
{%include“document_foot.html”%}
{%endblock%}
在不包含的情况下执行块没有任何区别

根据我的需要(页面和页脚),我从base.html重写了一些基本样式

我不确定我是否正确理解标题的功能,但我认为应该在每页上呈现标题(类似于MS Word标题),因为标题并不等于标题。如果我理解错误,则必须在每页上呈现标题

我的PDF的内容是动态的,其长度无法预测

我读过这本书,但没有找到解决问题的方法,我也没有找到任何解决方法

还请注意,我以横向方向呈现PDF


感谢您的帮助和建议。

找到了一个解决方案,如果其他人认为它有用,我将在这里发布

事实证明,我必须完全重写base.html并从中进行扩展。因此,我的new_base.html如下所示:

{% extends "easy_pdf/base.html" %}

{% block extra_style %}
<style type="text/css">

    @page {
        size: landscape;
        margin-left: 1cm;
        margin-right: 1cm;
        margin-top: 1.5cm;
        margin-bottom: 2cm;

        @frame footer {
            -pdf-frame-content: page-footer;
            bottom: 0cm;
            margin-left: 1cm;
            margin-right: 1cm;
            height: 2cm;
        }
    }

</style>
{% endblock %}

{% block page_header %}
    {% include "document_head.html" %}
{% endblock %}

{% block content %}
    {% include "main_table.html" %}
{% endblock %}

{% block page_foot %}
    {% include "document_foot.html" %}
{% endblock %}
<!DOCTYPE html>
<html>
<head>

    {% block style_base %}
        {% block layout_style %}
            <style type="text/css">

            </style>
        {%endblock%}
        {% block extra_style %}{% endblock %}
    {% endblock %}

</head>
<body>

    <div id="page-header">
        {%block page_header%}
        {%endblock%}
    </div>

    <div>
        {%block content%}
        {%endblock%}
    </div>

    <div id="page-footer">
        {%block page_foot%}
        {%endblock%}
    </div>
</body>

这会在文档的每一页上呈现页眉。

我最近也遇到了同样的问题,@user3745794给出的答案对我帮助很大

我的模板有一点不同,但在这里它们是为了防止它帮助有相同问题的人

模板/easy\u pdf/base.html

<!DOCTYPE html>
<html>
    <head>
        <title>{% block page_title %}{% endblock %}</title>

        {% block style_base %}
            {% block layout_style %}
                <style type="text/css">
                    @page {
                        size: {{ pagesize|default:"A4" }};
                        margin-left: 1cm;
                        margin-right: 1cm;

                        @frame header {
                            -pdf-frame-content: page-header;
                            margin-top: 1.0cm;
                            margin-left: 1cm;
                            margin-right: 0.5cm;
                            margin-bottom: 0.5cm;
                            height: 3cm;
                        }

                        @frame content {
                            top: 0.5cm;
                            margin-top: 3.5cm;
                            margin-bottom: 0.5cm;
                            margin-left: 1.75cm;
                            margin-right: 1.5cm;
                        }

                        @frame footer {
                            -pdf-frame-content: page-footer;
                            bottom: 0cm;
                            margin-left: 1cm;
                            margin-right: 1.5cm;
                            height: 2cm;
                        }
                    }
                </style>
            {%endblock%}
            {% block extra_style %}{% endblock %}
        {% endblock %}

    </head>

    <body>
        <div id="page-header">
            {%block page_header%}
            {%endblock%}
        </div>

        <div id="page-content">
            {%block page_content%}
            {%endblock%}
        </div>

        <div id="page-footer">
            {%block page_foot%}
            {%endblock%}
        </div>

    </body>
</html>
{% extends "easy_pdf/base.html" %}

{% load static %}

{% block page_title %}
    {# Page title here #}
{% endblock %}

{% block extra_style %}
    <style type="text/css">
        {# Extra CSS styles here #}
    </style>
{% endblock %}

{% block page_header %}
    {% include "header.html" %}
{% endblock %}

{% block page_content %}

    {# Page 1 #}

    <pdf:nextpage />

    {# Page 2 #}

    <pdf:nextpage />

    {# Page 3 #}

{% endblock %}

{% block page_foot %}
    {% include "footer.html" %}
{% endblock %}
<h1>Hello World!!!</h1>
<h1>Goodbye World!!!</h1>

{%block页面\标题%}{%endblock%}
{%block style_base%}
{%block LAYOU style%}
@页面{
大小:{pagesize |默认值:“A4”};
左边距:1厘米;
右边距:1cm;
@帧头{
-pdf框架内容:页眉;
边缘顶部:1.0cm;
左边距:1厘米;
右边距:0.5cm;
边缘底部:0.5cm;
高度:3厘米;
}
@框架内容{
顶部:0.5cm;
边缘顶部:3.5厘米;
边缘底部:0.5cm;
左边距:1.75厘米;
右边距:1.5cm;
}
@框架页脚{
-pdf框架内容:页脚;
底部:0厘米;
左边距:1厘米;
右边距:1.5cm;
高度:2厘米;
}
}
{%endblock%}
{%block-extra_-style%}{%endblock%}
{%endblock%}
{%block page_header%}
{%endblock%}
{%block page_content%}
{%endblock%}
{%block page_foot%}
{%endblock%}
在我的模板中模板/report.html

<!DOCTYPE html>
<html>
    <head>
        <title>{% block page_title %}{% endblock %}</title>

        {% block style_base %}
            {% block layout_style %}
                <style type="text/css">
                    @page {
                        size: {{ pagesize|default:"A4" }};
                        margin-left: 1cm;
                        margin-right: 1cm;

                        @frame header {
                            -pdf-frame-content: page-header;
                            margin-top: 1.0cm;
                            margin-left: 1cm;
                            margin-right: 0.5cm;
                            margin-bottom: 0.5cm;
                            height: 3cm;
                        }

                        @frame content {
                            top: 0.5cm;
                            margin-top: 3.5cm;
                            margin-bottom: 0.5cm;
                            margin-left: 1.75cm;
                            margin-right: 1.5cm;
                        }

                        @frame footer {
                            -pdf-frame-content: page-footer;
                            bottom: 0cm;
                            margin-left: 1cm;
                            margin-right: 1.5cm;
                            height: 2cm;
                        }
                    }
                </style>
            {%endblock%}
            {% block extra_style %}{% endblock %}
        {% endblock %}

    </head>

    <body>
        <div id="page-header">
            {%block page_header%}
            {%endblock%}
        </div>

        <div id="page-content">
            {%block page_content%}
            {%endblock%}
        </div>

        <div id="page-footer">
            {%block page_foot%}
            {%endblock%}
        </div>

    </body>
</html>
{% extends "easy_pdf/base.html" %}

{% load static %}

{% block page_title %}
    {# Page title here #}
{% endblock %}

{% block extra_style %}
    <style type="text/css">
        {# Extra CSS styles here #}
    </style>
{% endblock %}

{% block page_header %}
    {% include "header.html" %}
{% endblock %}

{% block page_content %}

    {# Page 1 #}

    <pdf:nextpage />

    {# Page 2 #}

    <pdf:nextpage />

    {# Page 3 #}

{% endblock %}

{% block page_foot %}
    {% include "footer.html" %}
{% endblock %}
<h1>Hello World!!!</h1>
<h1>Goodbye World!!!</h1>
{%extends“easy\u pdf/base.html”%}
{%load static%}
{%block page_title%}
{#此处的页面标题#}
{%endblock%}
{%block extra_style%}
{#这里有额外的CSS样式#}
{%endblock%}
{%block page_header%}
{%include“header.html”%}
{%endblock%}
{%block page_content%}
{第1页}
{第2页}
{第3页}
{%endblock%}
{%block page_foot%}
{%include“footer.html”%}
{%endblock%}
模板/header.html

<!DOCTYPE html>
<html>
    <head>
        <title>{% block page_title %}{% endblock %}</title>

        {% block style_base %}
            {% block layout_style %}
                <style type="text/css">
                    @page {
                        size: {{ pagesize|default:"A4" }};
                        margin-left: 1cm;
                        margin-right: 1cm;

                        @frame header {
                            -pdf-frame-content: page-header;
                            margin-top: 1.0cm;
                            margin-left: 1cm;
                            margin-right: 0.5cm;
                            margin-bottom: 0.5cm;
                            height: 3cm;
                        }

                        @frame content {
                            top: 0.5cm;
                            margin-top: 3.5cm;
                            margin-bottom: 0.5cm;
                            margin-left: 1.75cm;
                            margin-right: 1.5cm;
                        }

                        @frame footer {
                            -pdf-frame-content: page-footer;
                            bottom: 0cm;
                            margin-left: 1cm;
                            margin-right: 1.5cm;
                            height: 2cm;
                        }
                    }
                </style>
            {%endblock%}
            {% block extra_style %}{% endblock %}
        {% endblock %}

    </head>

    <body>
        <div id="page-header">
            {%block page_header%}
            {%endblock%}
        </div>

        <div id="page-content">
            {%block page_content%}
            {%endblock%}
        </div>

        <div id="page-footer">
            {%block page_foot%}
            {%endblock%}
        </div>

    </body>
</html>
{% extends "easy_pdf/base.html" %}

{% load static %}

{% block page_title %}
    {# Page title here #}
{% endblock %}

{% block extra_style %}
    <style type="text/css">
        {# Extra CSS styles here #}
    </style>
{% endblock %}

{% block page_header %}
    {% include "header.html" %}
{% endblock %}

{% block page_content %}

    {# Page 1 #}

    <pdf:nextpage />

    {# Page 2 #}

    <pdf:nextpage />

    {# Page 3 #}

{% endblock %}

{% block page_foot %}
    {% include "footer.html" %}
{% endblock %}
<h1>Hello World!!!</h1>
<h1>Goodbye World!!!</h1>
你好,世界!!!
模板/footer.html

<!DOCTYPE html>
<html>
    <head>
        <title>{% block page_title %}{% endblock %}</title>

        {% block style_base %}
            {% block layout_style %}
                <style type="text/css">
                    @page {
                        size: {{ pagesize|default:"A4" }};
                        margin-left: 1cm;
                        margin-right: 1cm;

                        @frame header {
                            -pdf-frame-content: page-header;
                            margin-top: 1.0cm;
                            margin-left: 1cm;
                            margin-right: 0.5cm;
                            margin-bottom: 0.5cm;
                            height: 3cm;
                        }

                        @frame content {
                            top: 0.5cm;
                            margin-top: 3.5cm;
                            margin-bottom: 0.5cm;
                            margin-left: 1.75cm;
                            margin-right: 1.5cm;
                        }

                        @frame footer {
                            -pdf-frame-content: page-footer;
                            bottom: 0cm;
                            margin-left: 1cm;
                            margin-right: 1.5cm;
                            height: 2cm;
                        }
                    }
                </style>
            {%endblock%}
            {% block extra_style %}{% endblock %}
        {% endblock %}

    </head>

    <body>
        <div id="page-header">
            {%block page_header%}
            {%endblock%}
        </div>

        <div id="page-content">
            {%block page_content%}
            {%endblock%}
        </div>

        <div id="page-footer">
            {%block page_foot%}
            {%endblock%}
        </div>

    </body>
</html>
{% extends "easy_pdf/base.html" %}

{% load static %}

{% block page_title %}
    {# Page title here #}
{% endblock %}

{% block extra_style %}
    <style type="text/css">
        {# Extra CSS styles here #}
    </style>
{% endblock %}

{% block page_header %}
    {% include "header.html" %}
{% endblock %}

{% block page_content %}

    {# Page 1 #}

    <pdf:nextpage />

    {# Page 2 #}

    <pdf:nextpage />

    {# Page 3 #}

{% endblock %}

{% block page_foot %}
    {% include "footer.html" %}
{% endblock %}
<h1>Hello World!!!</h1>
<h1>Goodbye World!!!</h1>
再见世界!!!
希望有帮助;)