问题
I am trying to export some data in a PDF file. I am using:
- Django 1.9.12
- django-easy-pdf 0.1.0
- Python 2.7
The export works fine and all (no problem with my view) but I am struggling with adding a page header into every page of the document.
At this point I can only render it on the first page. I have no such problem with the footer, it renders properly on every page.
My template is as follows:
{% 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 %}
Doing the blocks without include makes no difference.
I have rewritten some base styling from base.html according to my needs (page and footer).
I am not sure if I understand the functionality of the header properly but in my opinion it should be rendered on every page (similar to MS Word headers), because header does not equal title. If my understaning is wrong than there has to be another way to render a header on every page.
The content of my PDF is dynamic and it's length can not be predicted.
I have read the documentation and I have failed in finding the solution to my problem and I haven't found any solution here either.
Please also note that I render my PDF in landscape orientation.
Thank you for your help and suggestions.
回答1:
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.
回答2:
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 ;).
来源:https://stackoverflow.com/questions/41961365/django-easy-pdf-header-on-every-page-of-the-document