generate pdf from agenda and protocol
This commit is contained in:
@@ -9,4 +9,5 @@ urlpatterns = [
|
|||||||
path("", views.index, name="posts.index"),
|
path("", views.index, name="posts.index"),
|
||||||
path("fet_calendar.ics", views.calendar, name="posts.calendar"),
|
path("fet_calendar.ics", views.calendar, name="posts.calendar"),
|
||||||
path("<str:id>", views.show, name="posts.show"),
|
path("<str:id>", views.show, name="posts.show"),
|
||||||
|
path("<str:id>/<str:filename>", views.show_pdf, name="posts.show_pdf"),
|
||||||
]
|
]
|
||||||
|
|||||||
13
fet2020/posts/utils.py
Normal file
13
fet2020/posts/utils.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from django.http import HttpResponse
|
||||||
|
from io import BytesIO
|
||||||
|
from xhtml2pdf import pisa
|
||||||
|
|
||||||
|
|
||||||
|
def render_to_pdf(html):
|
||||||
|
result = BytesIO()
|
||||||
|
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
|
||||||
|
|
||||||
|
if not pdf.err:
|
||||||
|
return HttpResponse(result.getvalue(), content_type='application/pdf')
|
||||||
|
|
||||||
|
return None
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
from collections import deque
|
|
||||||
import logging
|
import logging
|
||||||
from taggit.models import Tag
|
|
||||||
|
|
||||||
from django.shortcuts import render
|
from collections import deque
|
||||||
|
from django.conf import settings
|
||||||
from django.http import HttpResponse, JsonResponse, HttpResponseServerError
|
from django.http import HttpResponse, JsonResponse, HttpResponseServerError
|
||||||
from django.utils.text import slugify
|
from django.shortcuts import render
|
||||||
|
from django.template.loader import render_to_string
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.utils.text import slugify
|
||||||
|
from taggit.models import Tag
|
||||||
|
|
||||||
from documents.api import get_pad_link
|
from documents.api import get_pad_link
|
||||||
from documents.etherpadlib import add_ep_cookie
|
from documents.etherpadlib import add_ep_cookie
|
||||||
from members.models import Member, JobMember
|
from members.models import Member, JobMember
|
||||||
|
|
||||||
from .models import Post, FetMeeting, FileUpload
|
from .models import Post, FetMeeting, FileUpload
|
||||||
|
from .utils import render_to_pdf
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -67,11 +70,17 @@ def tags(request, tag=""):
|
|||||||
return render(request, "posts/tag.html", context)
|
return render(request, "posts/tag.html", context)
|
||||||
|
|
||||||
|
|
||||||
def show(request, id=None):
|
def __get_post_object(id=None):
|
||||||
if id.isdigit() or id is int:
|
if id.isdigit() or id is int:
|
||||||
p = Post.objects.get(id=int(id))
|
return Post.objects.get(id=int(id))
|
||||||
elif id != "" and id is not None:
|
elif id != "" and id is not None:
|
||||||
p = Post.objects.get(slug=(id))
|
return Post.objects.get(slug=id)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def show(request, id=None):
|
||||||
|
p = __get_post_object(id)
|
||||||
|
|
||||||
files = deque(FileUpload.objects.filter(post=p))
|
files = deque(FileUpload.objects.filter(post=p))
|
||||||
|
|
||||||
@@ -85,10 +94,14 @@ def show(request, id=None):
|
|||||||
|
|
||||||
ep_agenda_link = "#"
|
ep_agenda_link = "#"
|
||||||
ep_protocol_link = "#"
|
ep_protocol_link = "#"
|
||||||
|
# set filename for pdf, not a nice solution
|
||||||
|
filename_agenda = None
|
||||||
|
filename_protocol = None
|
||||||
|
|
||||||
if p.has_agenda: # and p.agenda_key:
|
if p.has_agenda: # and p.agenda_key:
|
||||||
try:
|
try:
|
||||||
ep_agenda_link = get_pad_link(p.agenda_key)
|
ep_agenda_link = get_pad_link(p.agenda_key)
|
||||||
|
filename_agenda = p.slug + "-agenda.pdf"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Can't get the agenda link from '%s'. Error: %s", p.agenda_key, e
|
"Can't get the agenda link from '%s'. Error: %s", p.agenda_key, e
|
||||||
@@ -98,6 +111,7 @@ def show(request, id=None):
|
|||||||
if p.has_protocol: # and p.protocol_key:
|
if p.has_protocol: # and p.protocol_key:
|
||||||
try:
|
try:
|
||||||
ep_protocol_link = get_pad_link(p.protocol_key)
|
ep_protocol_link = get_pad_link(p.protocol_key)
|
||||||
|
filename_protocol = p.slug + "-protokoll.pdf"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Can't get the protocol link from '%s. Error: %s", p.protocol_key, e
|
"Can't get the protocol link from '%s. Error: %s", p.protocol_key, e
|
||||||
@@ -120,6 +134,8 @@ def show(request, id=None):
|
|||||||
"related_posts": related_posts[0:6],
|
"related_posts": related_posts[0:6],
|
||||||
"ep_agenda_link": ep_agenda_link,
|
"ep_agenda_link": ep_agenda_link,
|
||||||
"ep_protocol_link": ep_protocol_link,
|
"ep_protocol_link": ep_protocol_link,
|
||||||
|
"filename_agenda": filename_agenda,
|
||||||
|
"filename_protocol": filename_protocol,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = render(request, "posts/show.html", context)
|
response = render(request, "posts/show.html", context)
|
||||||
@@ -134,6 +150,30 @@ def show(request, id=None):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def show_pdf(self, id, filename=None):
|
||||||
|
p = __get_post_object(id)
|
||||||
|
|
||||||
|
# it works but not a nice solution
|
||||||
|
html = ""
|
||||||
|
if "agenda" in filename:
|
||||||
|
html = p.agenda_html
|
||||||
|
elif "protokoll" in filename:
|
||||||
|
html = p.protocol_html
|
||||||
|
|
||||||
|
rendered = render_to_string(settings.BASE_DIR + "/templates/posts/pdf/template.html")
|
||||||
|
|
||||||
|
# get body-content from pdf template
|
||||||
|
rendered = rendered.split("<html>")[1]
|
||||||
|
rendered = rendered.split("<body>")[0]
|
||||||
|
|
||||||
|
# set body-content in html
|
||||||
|
idx = html.index("<body>")
|
||||||
|
html = html[:idx] + rendered + html[idx:]
|
||||||
|
|
||||||
|
pdf = render_to_pdf(html)
|
||||||
|
return HttpResponse(pdf, content_type='application/pdf')
|
||||||
|
|
||||||
|
|
||||||
###########
|
###########
|
||||||
# HELPERS #
|
# HELPERS #
|
||||||
###########
|
###########
|
||||||
|
|||||||
13
fet2020/templates/posts/pdf/template.html
Normal file
13
fet2020/templates/posts/pdf/template.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
font-weight: 200;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -84,12 +84,23 @@
|
|||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
{% if post.has_agenda %}
|
{% if post.has_agenda %}
|
||||||
<a href="{{ ep_agenda_link }}">Agenda</a><br>
|
<a href="{{ ep_agenda_link }}">Agenda</a>
|
||||||
|
{% if filename_agenda %}
|
||||||
|
<a href="{% url 'posts.show_pdf' post.slug filename_agenda %}">
|
||||||
|
<i class="far fa-file-pdf"></i>
|
||||||
|
</a>
|
||||||
|
{% endif %}
|
||||||
|
<br>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if post.has_protocol %}
|
{% if post.has_protocol %}
|
||||||
<a href="{{ ep_protocol_link }}">Protokoll</a><br>
|
<a href="{{ ep_protocol_link }}">Protokoll</a>
|
||||||
|
{% if filename_protocol %}
|
||||||
|
<a href="{% url 'posts.show_pdf' post.slug filename_protocol %}">
|
||||||
|
<i class="far fa-file-pdf"></i>
|
||||||
|
</a>
|
||||||
|
{% endif %}
|
||||||
|
<br>
|
||||||
<hr>
|
<hr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user