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
{
'NAME': 'tex',
'BACKEND': 'django_tex.engine.TeXEngine',
'BACKEND': 'django_tex.engine.TeXEngine',
'DIRS': [
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
LATEX_INTERPRETER = latex_settings.get('interpreter', 'pdflatex')
LATEX_INTERPRETER_OPTIONS = latex_settings.get('options', '')
LATEX_GRAPHICSPATH = [
# Allow LaTeX files to access the report assets directory
os.path.join(MEDIA_ROOT, "report", "assets"),

View File

@ -82,3 +82,5 @@ latex:
# Note: The intepreter needs to be installed on the system!
# e.g. to install pdflatx: apt-get texlive-latex-base
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):
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.
@ -65,15 +72,18 @@ class ReportTemplate(models.Model):
filename = kwargs.get('filename', 'report.pdf')
context = self.get_context_data(request)
context['request'] = request
if self.extension == '.tex':
# Render LaTeX template to PDF
return render_to_pdf(request, self.template_name, context, filename=filename)
elif self.extension in ['.htm', '.html']:
# Render HTML template to PDF
wp = WeasyprintReportMixin(request, self.template_name, **kwargs)
return wp.render_to_response(context, **kwargs)
name = models.CharField(
blank=False, max_length=100,
help_text=_('Template name'),