From 5a8804f4bc1565235f1ccd49c90b7ef0c903c656 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 20 Aug 2020 13:57:29 +1000 Subject: [PATCH] If LaTeX template errors, return the raw LaTeX! --- InvenTree/report/models.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index c85fe8878f..fd27b2bc6e 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -30,6 +30,7 @@ if settings.LATEX_ENABLED: try: from django_tex.shortcuts import render_to_pdf from django_tex.core import render_template_with_context + from django_tex.exceptions import TexError except OSError as err: print("OSError: {e}".format(e=err)) print("You may not have a working LaTeX toolchain installed?") @@ -163,9 +164,14 @@ class ReportTemplateBase(models.Model): if self.extension == '.tex': # Render LaTeX template to PDF if settings.LATEX_ENABLED: - rendered = render_template_with_context(self.template_name, context) - #return TexResponse(rendered, filename="raw.tex") - return render_to_pdf(request, self.template_name, context, filename=filename) + # Attempt to render to LaTeX template + # If there is a rendering error, return the (partially rendered) template, + # 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: return ValidationError("Enable LaTeX support in config.yaml") elif self.extension in ['.htm', '.html']: