Django-easy-pdf, header on every page of the document

你说的曾经没有我的故事 提交于 2019-12-06 22:52:33

Found a solution and I shall post it here if anyone else finds it usefull.

It turned out that I had to completly rewrite the base.html and extend from it. So my new_base.html is as follows:

<!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>

And in my template which extends new_base.html I have added the following lines:

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

This now renders the page header on every page of the document.

I had the same issue recently and the answer given by @user3745794 helped me a lot.

My templates differ a little bit, but here they are in case it helps someone with the same issue.

templates/easy_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>

And in my template templates/report.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 %}

templates/header.html

<h1>Hello World!!!</h1>

templates/footer.html

<h1>Goodbye World!!!</h1>

Hope it helps ;).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!