Code Monkey home page Code Monkey logo

rest-framework-latex's Introduction

REST Framework LaTeX Plugin

CircleCI PyPI version Documentation Status

A simple plug-n-play LaTeX renderer for Django REST Framework.

Documentation

Installing

REST Framework LaTeX can be downloaded from PyPI:

pip install rest-framework-latex

Dependencies

Currently the LaTeX plugin requires lualatex - to install this on Ubuntu:

sudo aptitude install texlive-latex-extra texlive-xetex

This will probably take some time due to the size of LaTeX (around 1GB)

Using the Renderer

You can then configure the renderer in your settings or on each view:

REST_FRAMEWORK = {
  'DEFAULT_RENDERER_CLASSES': [
    'rest_framework_latex.renderers.LatexRenderer',
  ]
}

LATEX_RESOURCES Setting

The LATEX_RESOURCES directory contains the base template environment e.g. any images or static resources to include in your template. This must be set for the renderer to work:

LATEX_RESOURCES = '/home/user/path_to_resources'

This works just like TemplateHTMLRenderer but by setting a latex_name on your view:

from rest_framework import viewsets

from rest_framework_latex import renderers


class SomeViewSet(viewsets.ViewSet):
  """
  """
  renderer_classes = [
    renderers.LatexRenderer,
  ]

  latex_name = 'directory/latexfile.tex'

Latex Templates

To use the template tags, add rest_framework_latex to your INSTALLED_APPS:

INSTALLED_APPS = [
  ...
  'rest_framework_latex',
  ...
]

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 %}

Included Tags

Tag Tag/Filter Purpose
latex_safe Filter Escape all user-entered content for LaTeX rules
latex_resources Tag Print the value of settings.LATEX_RESOURCES

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.

Django Compatibility

The REST Framework LaTeX plugin is compatible with Django 1.11 and up and Django REST Framework 3.3 and up.

rest-framework-latex's People

Contributors

kennydude avatar mikehibbert avatar scott-w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rest-framework-latex's Issues

Open up TemplateTags

We have some template tags that make handling LaTeX files a little easier. We'll need to open them up and test them.

UnicodeEncodeError on export

When writing the PDF file, any unicode characters fail.

The answer is, in f.write, to .encode('utf-8') the string being written.

Ampersand in text causes error

Having an & in the outputted text (possibly through a variable) causes an error when running the file through LaTeX. We'll need to escape the & character properly.

Exception when luatex fails

When the luatex command fails, it should send its stderr output into the exception so we can figure out why it failed.

Table of Content Empty

When rendering a template like the following, the table on contents appears empty.

\documentclass{article}

\begin{document}

\tableofcontents

\section{Introduction}

example section

\end{document}

Test the renderer

We need to add some tests for the renderer output - the current tests for our app are integration tests, so we can't pull them out easily.

Django 1.10: ValueError: dictionary update sequence element #0 has length xx; 2 is required

Hi folks, just got first light on some sweet PDF output from your neat extension but I'm running into a weird bug in django-rest-framework.

Environment

  • Django 1.10.2
  • django-rest-swagger==2.0.7
  • djangorestframework==3.5.0
  • djangorestframework-csv==1.4.1
  • djangorestframework-filters==0.8.1
  • djangorestframework-gis==0.10.1
  • djangorestframework-jsonp==1.0.2
  • djangorestframework-yaml==1.0.3
  • dynamic-rest==1.5.0
  • rest-framework-latex==0.0.9

config:

TEMPLATES = [
    {
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
[...]

Expected

PDF output via rest-framework-latex

Error

Accessing an API view with rest-framework-latex's renderer enabled throws a

ValueError
dictionary update sequence element #0 has length 13; 2 is required

Hack

Change rest_framework/compat.py from

# backends template, e.g. django.template.backends.django.Template
    else:
        return template.render(context, request=request)

to

# backends template, e.g. django.template.backends.django.Template
    else:
        return template.render(dict(data=context), request=request)

so I can access it in my latex template like so

{% for obj in data %}
 {{ obj.some_field_that_my_api_exports|latex_safe }}
{% endfor %}

context is originally an OrderedDict of my queryset.
This is apparently absolutely required by all other renderers like the Browseable API renderer.
However, I couldn't figure out a way to access the OrderedDict in my template without wrapping it into a dict.

Unless I'm missing a straight-forward way to access the OrderedDict from the template, would it be possible to change LatexRenderer.render() to wrap data into a normal dict?

        tex = super(LatexRenderer, self).render(
            {"data": data}, accepted_media_type, renderer_context)

Sending PR for review and discussion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.