Skip to content

Commit

Permalink
replace WeasyPrint with pandoc
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Jul 16, 2016
1 parent 7b2c087 commit cb4c02b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
33 changes: 26 additions & 7 deletions apps/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from tempfile import mkstemp

from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.utils.six.moves.urllib.parse import urlparse

from weasyprint import HTML

import pypandoc

def get_script_alias(request):
return request.path[:-len(request.path_info)]
Expand Down Expand Up @@ -46,16 +48,33 @@ def get_internal_link(text, name, *args, **kwargs):
return "<a href=\"%s\">%s</a>" % (url, text)


def render_to_pdf(request, template_src, context_dict, filename):
def render_to_format(request, template_src, context_dict, title, format):
# render the template to a html string
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context).encode(encoding="UTF-8")

filename = filename + '.pdf'
# create a temporary file
(tmp_fd, tmp_filename) = mkstemp('.' + format)

# convert the file using pandoc
pypandoc.convert_text(html, format, format='html', outputfile=tmp_filename)

pdf_file = HTML(string=html, base_url=request.build_absolute_uri(), encoding="utf8").write_pdf()
# read the temporary file
file_handler = os.fdopen(tmp_fd)
file_content = file_handler.read()
file_handler.close()

response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = filename
# delete the temporary file
os.remove(tmp_filename)

# create the response object and return
response = HttpResponse(file_content, content_type='application/%s' % format)
response['Content-Disposition'] = 'attachment; filename="%s.%s"' % (title, format)
return response

def render_to_pdf(request, template_src, context_dict, title):
return render_to_format(request, template_src, context_dict, title, 'pdf')

def render_to_docx(request, template_src, context_dict, title):
return render_to_format(request, template_src, context_dict, title, 'docx')
19 changes: 17 additions & 2 deletions apps/projects/templates/projects/project_summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@ <h2>{% trans 'Options' %}</h2>

<ul class="list-unstyled">
<li>
<a href="{% url 'project_summary_pdf' project.pk %}" target="_blank">
{% trans 'Export PDF' %}
<a href="{% url 'project_summary_export' project.pk 'pdf' %}" target="_blank">
{% trans 'Export as PDF' %}
</a>
</li>
<li>
<a href="{% url 'project_summary_export' project.pk 'docx' %}" target="_blank">
{% trans 'Export as docx (Microsoft Office)' %}
</a>
</li>
<li>
<a href="{% url 'project_summary_export' project.pk 'docx' %}" target="_blank">
{% trans 'Export as odf (Open/Libre Office)' %}
</a>
</li>
<li>
<a href="{% url 'project_summary_export' project.pk 'tex' %}" target="_blank">
{% trans 'Export as LaTex source code' %}
</a>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion apps/projects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url(_(r'^(?P<pk>[0-9]+)/delete/$'), ProjectDeleteView.as_view(), name='project_delete'),

url(_(r'^(?P<pk>[0-9]+)/summary/$'), project_summary, name='project_summary'),
url(_(r'^(?P<pk>[0-9]+)/summary/pdf/$'), project_summary_pdf, name='project_summary_pdf'),
url(_(r'^(?P<pk>[0-9]+)/summary/(?P<format>[a-z]+)/$'), project_summary_export, name='project_summary_export'),

url(_(r'^(?P<project_id>[0-9]+)/questions/'), project_questions, name='project_questions'),
]
8 changes: 4 additions & 4 deletions apps/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework.status import HTTP_404_NOT_FOUND

from apps.core.views import ProtectedCreateView, ProtectedUpdateView, ProtectedDeleteView
from apps.core.utils import render_to_pdf
from apps.core.utils import render_to_format

from .models import Project
from .serializers import *
Expand Down Expand Up @@ -39,12 +39,12 @@ def project_summary(request, pk):


@login_required()
def project_summary_pdf(request, pk):
def project_summary_export(request, pk, format):
project = get_object_or_404(Project.objects.filter(owner=request.user), pk=pk)

return render_to_pdf(request, 'projects/project_summary_pdf.html', {
return render_to_format(request, 'projects/project_summary_pdf.html', {
'project': project
}, project.title)
}, project.title, format)


class ProjectCreateView(ProtectedCreateView):
Expand Down
3 changes: 1 addition & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ django-compressor>=2.0
django-libsass>=0.4
jsonfield>=1.0.0
django-bower>=5.1.0
CairoSVG==1.0.22
WeasyPrint>=0.29
pypandoc

0 comments on commit cb4c02b

Please sign in to comment.