0

I have a simple web-server written using Python Twisted. Users can log in and use it to generate certain reports (pdf-format), specific to that user. The report is made by having a .tex template file where I replace certain content depending on user, including embedding user-specific graphs (.png or similar), then use the command line program pdflatex to generate the pdf.

Currently the graphs are saved in a tmp folder, and that path is then put into the .tex template before calling pdflatex. But this probably opens up a whole pile of problems when the number of users increases, so I want to use temporary files (tempfile module) instead of a real tmp folder. Is there any way I can make pdflatex see these temporary files? Or am I doing this the wrong way?

funklute
  • 581
  • 4
  • 21
  • 1
    See http://stackoverflow.com/questions/18280245/where-does-python-tempfile-writes-its-files?rq=1. – Guntram Blohm Feb 26 '14 at 09:40
  • 4
    You can use `tempfile.NamedTemporaryFile`, if that's what you mean. Just make sure pdflatex is finished before you allow the file to remove itself. – Steve Jessop Feb 26 '14 at 09:41

2 Answers2

1

without any code it's hard to tell you how, but

Is there any way I can make pdflatex see these temporary files?

yes you can print the path to the temporary file by using a named temporary file:

>>> with tempfile.NamedTemporaryFile() as temp:
...     print temp.name
... 
/tmp/tmp7gjBHU
zmo
  • 24,463
  • 4
  • 54
  • 90
1

As commented you can use tempfile.NamedTemporaryFile. The problem is that this will be deleted once it is closed. That means you have to run pdflatex while the file is still referenced within python. As an alternative way you could just save the picture with a randomly generated name. The tempfile is designed to allow you to create temporary files on various platforms in a consistent way. This is not what you need, since you'll always run the script on the same webserver I guess.

You could generate random file names using the uuid module:

import uuid
for i in xrange(3):
    print(str(uuid.uuid4()))

The you save the pictures explictly using the random name and pass insert it into the tex-file. After running pdflatex you explicitly have to delete the file, which is the drawback of that approach.

Denis
  • 136
  • 6