Renderer
You can then configure the renderer in your settings or on each view:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework_latex.renderers.LatexRenderer',
]
}
# Or
from rest_framework import viewsets
from rest_framework_latex import renderers
class SomeViewSet(viewsets.ViewSet):
"""
"""
renderer_classes = [
renderers.LatexRenderer,
]
latex_name = 'directory/latexfile.tex'
The TeX file will be processed first through Django's templating system allowing you to dynamically alter the output.
Then it is sent through LuaLatex and the output PDF returned to the user.
You'll probably want external resources to be included, so a
LATEX_RESOURCES
directory needs to specified which includes
images etc you want to reference. (For example if you need
dog.jpg
in your document, put it in your LATEX_RESOURCES
directory and it'll get included properly)
Hooks
If you want to hook into the render process, there are a couple functions you can use to do so.
- pre_latex runs just before the Latex process is ran
- post_latex runs just after the Latex process, but before cleanup
from rest_framework import viewsets
from rest_framework_latex import renderers
class SomeViewSet(viewsets.ViewSet):
"""
"""
renderer_classes = [
renderers.LatexRenderer,
]
latex_name = 'directory/latexfile.tex'
def pre_latex(self, temp_dir, context):
pass
def post_latex(self, temp_dir, context):
pass
Django Templating
The TeX file used for rendering will be pushed through Django's templating system. This will cause some issues whereby you want to do something like:
\textt{{{ some_variable }}}
To get around this issue you will need to do something like the following:
\textt{% templatetag openbrace %}{{ some_variable }}{% templatetag closebrace %}
While the code is messy, it works well.
Included Tags
Tag | Tag/Filter | Purpose |
---|---|---|
latex_safe |
Filter | Escape all user-entered content for LaTeX rules |
latex_markdown |
Filter | Transform Markdown into LaTeX |
How it works
The renderer works by creating a new temporary directory, and then copying
over the LATEX_RESOURCES
directory into the new temporary directory.
Next it renders the TeX file into the temporary directory.
Then it runs lualatex over the TeX file, and this will produce the PDF file we read into memory.
Then we delete the temporary directory and return the PDF to the client.