2017-01-31 74 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> 

希望它有幫助;)。

相關問題