If LaTeX template errors, return the raw LaTeX!

This commit is contained in:
Oliver Walters 2020-08-20 13:57:29 +10:00
parent 2280558303
commit 5a8804f4bc

View File

@ -30,6 +30,7 @@ if settings.LATEX_ENABLED:
try: try:
from django_tex.shortcuts import render_to_pdf from django_tex.shortcuts import render_to_pdf
from django_tex.core import render_template_with_context from django_tex.core import render_template_with_context
from django_tex.exceptions import TexError
except OSError as err: except OSError as err:
print("OSError: {e}".format(e=err)) print("OSError: {e}".format(e=err))
print("You may not have a working LaTeX toolchain installed?") print("You may not have a working LaTeX toolchain installed?")
@ -163,9 +164,14 @@ class ReportTemplateBase(models.Model):
if self.extension == '.tex': if self.extension == '.tex':
# Render LaTeX template to PDF # Render LaTeX template to PDF
if settings.LATEX_ENABLED: if settings.LATEX_ENABLED:
rendered = render_template_with_context(self.template_name, context) # Attempt to render to LaTeX template
#return TexResponse(rendered, filename="raw.tex") # If there is a rendering error, return the (partially rendered) template,
return render_to_pdf(request, self.template_name, context, filename=filename) # so at least we can debug what is going on
try:
rendered = render_template_with_context(self.template_name, context)
return render_to_pdf(request, self.template_name, context, filename=filename)
except TexError:
return TexResponse(rendered, filename="error.tex")
else: else:
return ValidationError("Enable LaTeX support in config.yaml") return ValidationError("Enable LaTeX support in config.yaml")
elif self.extension in ['.htm', '.html']: elif self.extension in ['.htm', '.html']: