i have a small Django app to manage my patients data, and use LaTeX to output reports. It consists un a parent model called Patients and child models Consultation , StressEcho and so on. With LaTex this works without problems

def courrier_pdf(request, pk2, pk1):
entry = Courrier.objects.get(pk=pk2)
source = Patient.objects.get(pk=pk1)
context = dict({'courrier': entry, 'patient': source})
#buffer = BytesIO()
template = get_template('courrier/courrier.tex')
rendered_tpl = template.render(context, request).encode('utf-8')
#Python3 only. For python2 check out the docs!
with tempfile.TemporaryDirectory() as tempdir:
  # Create subprocess, supress output with PIPE and
  # run latex twice to generate the TOC properly.
  # Finally read the generated pdf.
    for i in range(2):
        process = Popen(
            ['xelatex', '-output-directory', tempdir],
            stdin=PIPE,
            stdout=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)
return r

i tried with this view in order to use ConTeXT but without success

def courrier_mkiv(request, pk2, pk1):

entry = Courrier.objects.get(pk=pk2)
cource = Patient.objects.get(pk=pk1)
context = dict({'courrier': entry, 'patient': cource})
# buffer = BytesIO()
template = get_template('courrier/courrier.mkiv')
rendered_tpl = template.render(context, request).encode('utf-8')
with tempfile.TemporaryDirectory() as tempdir:
    process = Popen(['context', '--result', tempdir],
                    stdin=PIPE,
                    stdout=PIPE,
                    )
    process.communicate(rendered_tpl)
    with open(os.path.join(tempdir, 'textput.pdf'), 'rb') as f:
        pdf = f.read()
r = HttpResponse(content_type='application/pdf')
r.write(pdf)
return r

My feeling is that the problem is in the workspace since i get logfiles in the root of my app Here is log file from ConTeXT:

open source     > level 1, order 1, name '/home/kaddour/context/tex/texmf-
context/tex/context/base/mkiv/cont-yes.mkiv'
system          > 
system          > ConTeXt  ver: 2018.01.04 17:37 MKIV beta  fmt: 2018.1.4  int: english/english
system          > 
system          > 'cont-new.mkiv' loaded
open source     > level 2, order 2, name '/home/kaddour/context/tex/texmf-
context/tex/context/base/mkiv/cont-new.mkiv'
close source    > level 2, order 2, name '/home/kaddour/context/tex/texmf-
context/tex/context/base/mkiv/cont-new.mkiv'
system          > files > jobname 'tmpy82c5j7i', input '/tmp/tmpy82c5j7i', result 'tmpy82c5j7i'
fonts           > latin modern fonts are not preloaded
languages       > language 'en' is active

tex error       > tex error on line 8 in file /home/kaddour/context/tex/texmf-
context/tex/context/base/mkiv/cont-yes.mkiv: ! I can't find file `/tmp/tmpy82c5j7i'.

l.8 }

I tried this in my view

os.chdirw(tempdir)

just before process without success.

My guess is that ConTeXT option --result= does not behave exactly like LaTeX's -output-directoryAny help is welcome i really need ConTeXT for my work and this problem is driving me mad!


--
Dr YAHYAOUI Mohamed Kaddour, cardiologue -- Clinique KARDIA.