Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!