2017-01-31 34 views
1

我试图导出PDF文件中的一些数据。我使用:Django-easy-pdf,文档的每个页面上的标题

  • Django的1.9.12
  • Django的易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。

谢谢你的帮助和建议。

回答

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

现在,这使得每个页头文档的页面。

0

我最近有同样的问题,@ 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> 

在我的模板模板/ 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 %} 

模板/ header.html中

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

模板/ footer.html

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

希望它有帮助;)。

相关问题