|
1 | 1 | import os |
2 | | -from django.conf import settings |
3 | | -from base.models.coursebook import Favorite |
4 | | -from content.models import CONTENT_TYPES |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +from subprocess import Popen, PIPE |
5 | 5 |
|
| 6 | +from django.template.loader import get_template |
6 | 7 |
|
7 | | -def generate_coursebook_for(user, course): |
8 | | - """ |
9 | | - Generates a pdf file of a coursebook |
10 | | - :param user: the user which exports the coursebook |
11 | | - :param course: the course containing the coursebook |
12 | | - :return: filepath of the generated pdf file |
13 | | - """ |
14 | | - file_directory = os.path.join(settings.BASE_DIR, "media", "coursebooks") |
15 | | - filename = "Coursebook_" + str(user) + "_" + str(course) |
16 | | - file_path = os.path.join(file_directory, filename) |
17 | | - |
18 | | - if not os.path.exists(file_directory): |
19 | | - os.makedirs(file_directory) |
20 | | - |
21 | | - latex_document_string = generate_latex_head(course.title, user) |
22 | | - |
23 | | - # sort contents by topic |
24 | | - contents = [favorite.content for favorite in Favorite.objects.filter(user=user.profile, course=course)] |
25 | | - contents = sorted(contents, key=lambda c: c.topic.title) |
26 | | - |
27 | | - # save the last topic to generate a new section for each topic |
28 | | - last_topic = None |
29 | | - for content in contents: |
30 | | - content_type = content.type |
31 | | - if last_topic != content.topic: |
32 | | - last_topic = content.topic |
33 | | - latex_document_string += generate_latex_section(content.topic.title) |
34 | | - |
35 | | - if content_type in CONTENT_TYPES: |
36 | | - content_type_object = CONTENT_TYPES.get(content_type).objects.get(pk=content.pk) |
37 | | - latex_document_string += content_type_object.generate_latex_template() |
38 | | - else: |
39 | | - latex_document_string += generate_latex_invalid_content_type(content_type) |
40 | | - |
41 | | - latex_document_string += generate_latex_end() |
42 | | - |
43 | | - # write data into tex file |
44 | | - with open(file_path + '.tex', 'w+') as file: |
45 | | - file.write(latex_document_string) |
46 | | - file.close() |
47 | | - |
48 | | - # convert tex file into pdf |
49 | | - bash_convert = "pdflatex -no-file-line-error -halt-on-error -output-directory='" + \ |
50 | | - file_directory + "' " \ |
51 | | - + file_path + ".tex" |
52 | | - bash_quiet = " > /dev/null" # Quiet output in the shell |
53 | | - os.system(bash_convert + bash_quiet) # run as shell command |
54 | | - |
55 | | - # remove unnecessary files to save space |
56 | | - os.remove(file_path + ".tex") |
57 | | - os.remove(file_path + ".aux") |
58 | | - os.remove(file_path + ".log") |
59 | | - os.remove(file_path + ".out") |
60 | 8 |
|
61 | | - return file_path |
62 | | - |
63 | | - |
64 | | -def generate_latex_head(title, author): |
65 | | - return r"""\documentclass{article} |
66 | | - \usepackage[T1]{fontenc} |
67 | | - \usepackage[utf8]{inputenc} |
68 | | - \usepackage{graphicx} |
69 | | - \usepackage{float} |
70 | | - \usepackage{grffile} |
71 | | - \usepackage{hyperref} |
72 | | -
|
73 | | - \title{""" + str(title) + r""" - Coursebook} |
74 | | - \author{""" + str(author) + r"""} |
75 | | - \date{\today} |
76 | | -
|
77 | | - \begin{document} |
78 | | - \maketitle |
| 9 | +class LaTeX: |
79 | 10 | """ |
80 | | - |
81 | | - |
82 | | -def generate_latex_end(): |
83 | | - return r"""\end{document}""" |
84 | | - |
85 | | - |
86 | | -def generate_latex_invalid_content_type(content_type): |
87 | | - return r"""\textit{This type of content is not supported and cannot be displayed: """ + str(content_type) + """}""" |
88 | | - |
89 | | - |
90 | | -def generate_latex_section(title): |
91 | | - return r"\section{" + str(title) + "}" |
| 11 | + https://github.com/d120/pyophase/blob/master/ophasebase/helper.py |
| 12 | + Retrieved 10.08.2020 |
| 13 | + """ |
| 14 | + @staticmethod |
| 15 | + def render(context, template_name, assets, app='export', external_assets=None): |
| 16 | + template = get_template(template_name) |
| 17 | + rendered_tpl = template.render(context).encode('utf-8') |
| 18 | + with tempfile.TemporaryDirectory() as tempdir: |
| 19 | + # for asset in assets: |
| 20 | + # shutil.copy(os.path.dirname(os.path.realpath(__file__)) + '/../' + app + '/assets/' + asset, tempdir) |
| 21 | + # if external_assets is not None: |
| 22 | + # for asset in external_assets: |
| 23 | + # shutil.copy(asset, tempdir) |
| 24 | + process = Popen(['pdflatex'], stdin=PIPE, stdout=PIPE, cwd=tempdir, ) |
| 25 | + pdflatex_output = process.communicate(rendered_tpl) |
| 26 | + try: |
| 27 | + with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f: |
| 28 | + pdf = f.read() |
| 29 | + except FileNotFoundError: |
| 30 | + pdf = None |
| 31 | + return pdf, pdflatex_output |
0 commit comments