Django: using ConTeXT mkiv to output PDF
i have a small Django app https://bitbucket.org/kaddourkardio/kardiology 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.
Am 2018-01-24 um 15:15 schrieb kaddour kardio
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
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!
Yes, --result doesn’t work with directories, just filenames. In my own Django app that uses ConTeXt, I just copy/generate all the needes files into one work directory per process. Greetlings, Hraban --- http://www.fiee.net http://wiki.contextgarden.net GPG Key ID 1C9B22FD
Can you please give me a snippet on how you manage working in another
directory ?
On Thursday, January 25, 2018, Henning Hraban Ramm
Am 2018-01-24 um 15:15 schrieb kaddour kardio
: 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
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!
Yes, --result doesn’t work with directories, just filenames.
In my own Django app that uses ConTeXt, I just copy/generate all the needes files into one work directory per process.
Greetlings, Hraban --- http://www.fiee.net http://wiki.contextgarden.net GPG Key ID 1C9B22FD
____________________________________________________________ _______________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/ listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ____________________________________________________________ _______________________
-- Dr YAHYAOUI Mohamed Kaddour, cardiologue -- Clinique KARDIA.
On 1/25/2018 2:58 PM, kaddour kardio wrote:
Can you please give me a snippet on how you manage working in another directory ?
mtxrun --path=/data/foo --script context .....
On Thursday, January 25, 2018, Henning Hraban Ramm
mailto:texml@fiee.net> wrote: Am 2018-01-24 um 15:15 schrieb kaddour kardio
mailto:kaddourkardio@gmail.com>: > 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
> 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!
Yes, --result doesn’t work with directories, just filenames.
In my own Django app that uses ConTeXt, I just copy/generate all the needes files into one work directory per process.
Greetlings, Hraban --- http://www.fiee.net http://wiki.contextgarden.net GPG Key ID 1C9B22FD
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl mailto:ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
-- Dr YAHYAOUI Mohamed Kaddour, cardiologue -- Clinique KARDIA.
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
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: # mtxrun --path=/data/foo --script context process = Popen(['mtxrun', '--path', tempdir, '--script=context' '--result'], 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
i tried this without success :( the only workaround u've found for now is to render a tex file and copile it manually.
On 1/26/2018 7:41 PM, kaddour kardio wrote:
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: # mtxrun --path=/data/foo --script context process = Popen(['mtxrun', '--path', tempdir, '--script=context' '--result'], 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
i tried this without success :( the only workaround u've found for now is to render a tex file and copile it manually.
I have no clue what you do, but just running context should nto be that hard. And yes, you need a file (no piping of stdin) as there are normally multiple runs needes. Also, mkiv assumes that there is a file (tex, xml, ...) as it's actually an indirect process: if you run "context foo.tex" on the console you will see what gets actually called and the file loaded is "cont-yes.mkiv" which in turn will load whatever is needed as source. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 01/26/2018 07:41 PM, kaddour kardio wrote:
[...] i tried this without success :( the only workaround u've found for now is to render a tex file and copile it manually.
I don’t know how to invoke ConTeXt using Django, but to create booklets with my favourite text editor (Geany, https://geany.org/). While --result could handle path in it, my command to create booklets was: contextjit --purgeall --path="%d" --arguments=filename=%e.pdf --result=/home/ousia/"%e-2UP.pdf" /home/ousia/A5-to-A4_landscape-booklet.tex "%d" and %e" stand for directory and file name without extension. After --path was introduced (or it was mandatory to use it, since --result cannot handle path in it), my command reads: cd && contextjit --purgeall --path="%d" --arguments=filename=%d/%e.pdf --result="%e-2UP.pdf" /home/ousia/A5-to-A4_landscape-booklet.tex I wonder whether my previous comment might help you to improve your ConTeXt in Django. Best wishes, Pablo -- http://www.ousia.tk
i think i am one step closer to my goal. alla i have is to combine: 1- What Hans taught me about working on file (and not simply stdout) python has already a module *tempfile.NamedTemporaryFile()* , 2- Your snippet to run ConTeXT inside the temporary folder *cd && contextjit --purgeall --path="%d" --arguments=filename=%d/%e.pdf* *--result="%e-2UP.pdf" /home/ousia/A5-to-A4_landscape-booklet.tex* **To be adapted for a working example ( tomorrow i have a night shift so probably tuesday i will work on again). Thank you everybody!
Still working on the issue, i have a simple (comehow related) question:
since the PDF generated is a binary, i can pipe it through the stdout ?
On Mon, Jan 29, 2018 at 12:33 AM, kaddour kardio
i think i am one step closer to my goal. alla i have is to combine: 1- What Hans taught me about working on file (and not simply stdout) python has already a module *tempfile.NamedTemporaryFile()* , 2- Your snippet to run ConTeXT inside the temporary folder *cd && contextjit --purgeall --path="%d" --arguments=filename=%d/%e.pdf* *--result="%e-2UP.pdf" /home/ousia/A5-to-A4_landscape-booklet.tex*
**To be adapted for a working example ( tomorrow i have a night shift so probably tuesday i will work on again).
Thank you everybody!
-- Dr YAHYAOUI Mohamed Kaddour, cardiologue -- Clinique KARDIA.
On 2/2/2018 1:22 PM, kaddour kardio wrote:
Still working on the issue, i have a simple (comehow related) question: since the PDF generated is a binary, i can pipe it through the stdout ? no, because in some cases the file is rewritten (for instance the stream length is only known after the compressed stream is written and updated afterwards)
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (4)
-
Hans Hagen
-
Henning Hraban Ramm
-
kaddour kardio
-
Pablo Rodriguez