Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
Django latex template with images

I'm trying to generate pdf from django latex template. To do so I'm using the code from here:

So I have this code in views.py

context = {....}
template = get_template('my_latex_template.tex')
rendered_tpl = template.render(context).encode('utf-8')
with tempfile.TemporaryDirectory() as tempdir:  
    for ppp in range(2):
        process = Popen(
            ['pdflatex', '-output-directory', tempdir],
            stdin=PIPE,
            stdout=PIPE,
            stderr=PIPE,
        )
        process.communicate(rendered_tpl)
    with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f:
        pdf = f.read()
r = HttpResponse(content_type='application/pdf')  
r.write(pdf)

Here is my_latex_template.tex

{% autoescape on %}
\documentclass[a4paper,12pt]{article} 
\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{graphicx}

\begin{document}
 blabla
\includegraphics{img.png}
\end{document}
{% endautoescape %}

The structure of my directories:

myapp
|..templates
|..|..my_latex_template.tex
|..|..img.png
|..views.py
...

When compiling this latex template without \includegraphics{img.png}, everything works perfect. When compiling it with the image, I'm getting error that

[Errno 2] No such file or directory: '/var/folders/73/r8hl47ld11l68v_1kjh_m61m0000gn/T/tmprfp7wf_x/texput.pdf'

That basically means that this temporary file is not generating correctly by running pdflatex on rendered template...


Interesting that when I'm doing following in my_latex_template.tex

{% autoescape on %}
\documentclass[a4paper,12pt]{article} 
\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{graphicx}

\begin{document}
blabla
{% endautoescape %}
\includegraphics{img.png}
{% autoescape on %}
\end{document}
{% endautoescape %}

The code compiles correctly and I'm getting pdf, but instead of image I'm getting this -- picture doesn't show correctly


Do you have any ideas on what can I do? I would be super grateful if someone could help me.

Thank you

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda