Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda