【问题标题】:Django-easy-pdf, header on every page of the documentDjango-easy-pdf,文档每一页的页眉
【发布时间】:2017-01-31 15:31:13
【问题描述】:

我正在尝试在 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 %}

不包含块没有任何区别。

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

我不确定我是否正确理解了标题的功能,但我认为它应该呈现在每个页面上(类似于 MS Word 标题),因为 标题不等于标题。 如果我的理解是错误的,必须有另一种方式在每个页面上呈现标题。

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

我已阅读documentation,但未能找到解决问题的方法,我也没有在这里找到任何解决方案。

另请注意,我以横向方向呈现我的 PDF。

感谢您的帮助和建议。

【问题讨论】:

    标签: django pdf django-templates


    【解决方案1】:

    找到一个解决方案,如果其他人觉得有用,我会在这里发布。

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

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

    在扩展 new_base.html 的模板中,我添加了以下几行:

    @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;
    }
    

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

    【讨论】:

      【解决方案2】:

      我最近遇到了同样的问题,@user3745794 的回答对我帮助很大。

      我的模板略有不同,但在这里它们可以帮助遇到相同问题的人。

      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>
      

      在我的模板中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>
      

      希望它有所帮助;)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 2017-05-26
        • 1970-01-01
        相关资源
        最近更新 更多