1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from errno import ENOENT
8 from django.conf import settings
10 from django.http import HttpResponse
11 from wolnelektury.utils import makedirs
13 logger = logging.getLogger(__name__)
16 def render_to_pdf(output_path, template, context=None, add_files=None):
17 """Renders a TeXML document into a PDF file.
19 :param str output_path: is where the PDF file should go
20 :param str template: is a TeXML template path
21 :param context: is context for rendering the template
22 :param dict add_files: a dictionary of additional files XeTeX will need
25 from StringIO import StringIO
27 from tempfile import mkdtemp
29 import Texml.processor
30 from django.template.loader import render_to_string
32 rendered = render_to_string(template, context)
33 texml = StringIO(rendered.encode('utf-8'))
34 tempdir = mkdtemp(prefix="render_to_pdf-")
35 tex_path = os.path.join(tempdir, "doc.tex")
36 with open(tex_path, 'w') as tex_file:
37 Texml.processor.process(texml, tex_file, encoding="utf-8")
40 for add_name, src_file in add_files.items():
41 add_path = os.path.join(tempdir, add_name)
42 if hasattr(src_file, "read"):
43 with open(add_path, 'w') as add_file:
44 add_file.write(add_file.read())
46 shutil.copy(src_file, add_path)
51 subprocess.check_call(
52 ['xelatex', '-interaction=batchmode', tex_path],
53 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54 makedirs(os.path.dirname(output_path))
55 shutil.move(os.path.join(tempdir, "doc.pdf"), output_path)
58 shutil.rmtree(tempdir)
61 def render_to_csv(output_path, template, context=None, add_files=None):
62 """Renders a TeXML document into a PDF file.
64 :param str output_path: is where the PDF file should go
65 :param str template: is a TeXML template path
66 :param context: is context for rendering the template
67 :param dict add_files: a dictionary of additional files XeTeX will need
70 from django.template.loader import render_to_string
72 makedirs(os.path.dirname(output_path))
74 rendered = render_to_string(template, context)
75 with open(output_path, 'w') as csv_file:
76 csv_file.write(rendered.encode('utf-8'))
79 def read_chunks(f, size=8192):
86 def generated_file_view(file_name, mime_type, send_name=None, signals=None):
87 file_path = os.path.join(settings.MEDIA_ROOT, file_name)
89 send_name = os.path.basename(file_name)
91 def signal_handler(*args, **kwargs):
95 if oe.errno != ENOENT:
99 for signal in signals:
100 signal.connect(signal_handler, weak=False)
103 def view(request, *args, **kwargs):
104 if not os.path.exists(file_path):
105 func(file_path, *args, **kwargs)
107 if hasattr(send_name, "__call__"):
112 response = HttpResponse(content_type=mime_type)
113 response['Content-Disposition'] = 'attachment; filename=%s' % name
114 with open(file_path) as f:
115 for chunk in read_chunks(f):
116 response.write(chunk)