This commit is contained in:
Oliver Walters 2020-05-22 00:09:51 +10:00
parent cab87a6860
commit 251a23d127
3 changed files with 17 additions and 3 deletions

View File

@ -203,7 +203,7 @@ TEMPLATES = [
# Backend for LaTeX report rendering # Backend for LaTeX report rendering
{ {
'NAME': 'tex', 'NAME': 'tex',
'BACKEND': 'django_tex.engine.TeXEngine', 'BACKEND': 'django_tex.engine.TeXEngine',
'DIRS': [ 'DIRS': [
os.path.join(MEDIA_ROOT, 'report'), os.path.join(MEDIA_ROOT, 'report'),
] ]
@ -356,6 +356,8 @@ latex_settings = CONFIG.get('latex', {})
# Set the latex interpreter in the config.yaml settings file # 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_GRAPHICSPATH = [ LATEX_GRAPHICSPATH = [
# Allow LaTeX files to access the report assets directory # Allow LaTeX files to access the report assets directory
os.path.join(MEDIA_ROOT, "report", "assets"), os.path.join(MEDIA_ROOT, "report", "assets"),

View File

@ -82,3 +82,5 @@ latex:
# Note: The intepreter needs to be installed on the system! # Note: The intepreter needs to be installed on the system!
# e.g. to install pdflatx: apt-get texlive-latex-base # e.g. to install pdflatx: apt-get texlive-latex-base
interpreter: pdflatex interpreter: pdflatex
# Extra options to pass through to the LaTeX interpreter
options: ''

View File

@ -54,7 +54,14 @@ class ReportTemplate(models.Model):
def template_name(self): def template_name(self):
return os.path.join('report_template', os.path.basename(self.template.name)) return os.path.join('report_template', os.path.basename(self.template.name))
def render(self, request, context={}, **kwargs): def get_context_data(self, request):
"""
Supply context data to the template for rendering
"""
return {}
def render(self, request, **kwargs):
""" """
Render the template to a PDF file. Render the template to a PDF file.
@ -65,15 +72,18 @@ class ReportTemplate(models.Model):
filename = kwargs.get('filename', 'report.pdf') filename = kwargs.get('filename', 'report.pdf')
context = self.get_context_data(request)
context['request'] = request context['request'] = request
if self.extension == '.tex': if self.extension == '.tex':
# Render LaTeX template to PDF
return render_to_pdf(request, self.template_name, context, filename=filename) return render_to_pdf(request, self.template_name, context, filename=filename)
elif self.extension in ['.htm', '.html']: elif self.extension in ['.htm', '.html']:
# Render HTML template to PDF
wp = WeasyprintReportMixin(request, self.template_name, **kwargs) wp = WeasyprintReportMixin(request, self.template_name, **kwargs)
return wp.render_to_response(context, **kwargs) return wp.render_to_response(context, **kwargs)
name = models.CharField( name = models.CharField(
blank=False, max_length=100, blank=False, max_length=100,
help_text=_('Template name'), help_text=_('Template name'),