diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 8e4feade23..e5b14314b5 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -351,12 +351,15 @@ DATE_INPUT_FORMATS = [ ] # LaTeX rendering settings (django-tex) -latex_settings = CONFIG.get('latex', {}) +LATEX_SETTINGS = CONFIG.get('latex', {}) + +# Is LaTeX rendering enabled? (Off by default) +LATEX_ENABLED = LATEX_SETTINGS.get('enabled', False) # Set the latex interpreter in the config.yaml settings file -LATEX_INTERPRETER = latex_settings.get('interpreter', 'pdflatex') +LATEX_INTERPRETER = LATEX_SETTINGS.get('interpreter', 'pdflatex') -LATEX_INTERPRETER_OPTIONS = latex_settings.get('options', '') +LATEX_INTERPRETER_OPTIONS = LATEX_SETTINGS.get('options', '') LATEX_GRAPHICSPATH = [ # Allow LaTeX files to access the report assets directory diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 5cb0b1073c..5447606337 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -77,10 +77,12 @@ sentry: # LaTeX report rendering # InvenTree uses the django-tex plugin to enable LaTeX report rendering # Ref: https://pypi.org/project/django-tex/ +# Note: Ensure that a working LaTeX toolchain is installed and working *before* starting the server latex: # Select the LaTeX interpreter to use for PDF rendering # Note: The intepreter needs to be installed on the system! # e.g. to install pdflatex: apt-get texlive-latex-base + enabled: False interpreter: pdflatex # Extra options to pass through to the LaTeX interpreter options: '' \ No newline at end of file diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 2ab12ad258..93f1f1a95f 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -6,8 +6,11 @@ Report template model definitions from __future__ import unicode_literals import os +import sys from django.db import models +from django.conf import settings + from django.core.validators import FileExtensionValidator from django.core.exceptions import ValidationError @@ -15,8 +18,21 @@ from django.utils.translation import gettext_lazy as _ from part import models as PartModels -from django_tex.shortcuts import render_to_pdf -from django_weasyprint import WeasyTemplateResponseMixin +try: + from django_weasyprint import WeasyTemplateResponseMixin +except OSError as err: + print("OSError: {e}".format(e=err)) + print("You may require some further system packages to be installed.") + sys.exit(1) + +# Conditional import if LaTeX templating is enabled +if settings.LATEX_ENABLED: + try: + from django_tex.shortcuts import render_to_pdf + except OSError as err: + print("OSError: {e}".format(e=err)) + print("You may not have a working LaTeX toolchain installed?") + sys.exit(1) def rename_template(instance, filename): @@ -137,7 +153,10 @@ class ReportTemplateBase(models.Model): if self.extension == '.tex': # Render LaTeX template to PDF - return render_to_pdf(request, self.template_name, context, filename=filename) + if settings.LATEX_ENABLED: + return render_to_pdf(request, self.template_name, context, filename=filename) + else: + return ValidationError("Enable LaTeX support in config.yaml") elif self.extension in ['.htm', '.html']: # Render HTML template to PDF wp = WeasyprintReportMixin(request, self.template_name, **kwargs)