如何使用LaTeX以编程方式生成PDF?

时间:2022-01-19 21:10:52

I'm trying to generate some LaTeX code which from thereon should generate PDF documents. Currently, I'm using the Django templating system for dynamically creating the code, but I have no idea on as how to move on from here. I understand that I could save the code in a .tex file, and use subprocess to run pdflatex for generating the PDF. But I had so much trouble escaping the LaTeX code in "plain" Python that I decided to use the Django templating system. Is there a way that I could somehow maybe pipe the output produced by Django to pdflatex? The code produced is working properly, it's just that I do not know what to do with it.

我正在尝试生成一些LaTeX代码,从那里生成PDF文档。目前,我正在使用Django模板系统动态创建代码,但我不知道如何继续从这里开始。我知道我可以将代码保存在.tex文件中,并使用子进程运行pdflatex来生成PDF。但是我在“普通”Python中逃避LaTeX代码时遇到了很多麻烦,我决定使用Django模板系统。有没有办法我可以以某种方式将Django生成的输出传递给pdflatex?生成的代码工作正常,只是我不知道如何处理它。

Thanks in advance

提前致谢

1 个解决方案

#1


5  

I was tackling the same issue in a project I had previously worked on, and instead of piping the output, I created temporary files in a temporary folder, since I was worried about handling the intermediate files LaTeX produces. This is the code I used (note that it's a few years old, from when I was still new to Python/Django; I'm sure I could come up with something better if I was writing this today, but this definitely worked for me):

我正在处理我之前处理过的项目中的相同问题,而不是管道输出,我在临时文件夹中创建了临时文件,因为我担心处理LaTeX生成的中间文件。这是我使用的代码(注意它已经有几年了,从我还是Python / Django的新手开始;我确信如果我今天写这篇文章的话我会想出更好的东西,但这对我来说肯定有用):

import os
from subprocess import call
from tempfile import mkdtemp, mkstemp
from django.template.loader import render_to_string
# In a temporary folder, make a temporary file
tmp_folder = mkdtemp()
os.chdir(tmp_folder)        
texfile, texfilename = mkstemp(dir=tmp_folder)
# Pass the TeX template through Django templating engine and into the temp file
os.write(texfile, render_to_string('tex/base.tex', {'var': 'whatever'}))
os.close(texfile)
# Compile the TeX file with PDFLaTeX
call(['pdflatex', texfilename])
# Move resulting PDF to a more permanent location
os.rename(texfilename + '.pdf', dest_folder)
# Remove intermediate files
os.remove(texfilename)
os.remove(texfilename + '.aux')
os.remove(texfilename + '.log')
os.rmdir(tmp_folder)
return os.path.join(dest_folder, texfilename + '.pdf')

The dest_folder variable is usually set to somewhere in the media directory, so that the PDF can then be served statically. The value returned is the path to the file on disk. The logic of what its URL would be is handled by whatever function sets the dest_folder.

dest_folder变量通常设置在media目录中的某个位置,以便可以静态地提供PDF。返回的值是磁盘上文件的路径。它的URL的逻辑由设置dest_folder的任何函数处理。

#1


5  

I was tackling the same issue in a project I had previously worked on, and instead of piping the output, I created temporary files in a temporary folder, since I was worried about handling the intermediate files LaTeX produces. This is the code I used (note that it's a few years old, from when I was still new to Python/Django; I'm sure I could come up with something better if I was writing this today, but this definitely worked for me):

我正在处理我之前处理过的项目中的相同问题,而不是管道输出,我在临时文件夹中创建了临时文件,因为我担心处理LaTeX生成的中间文件。这是我使用的代码(注意它已经有几年了,从我还是Python / Django的新手开始;我确信如果我今天写这篇文章的话我会想出更好的东西,但这对我来说肯定有用):

import os
from subprocess import call
from tempfile import mkdtemp, mkstemp
from django.template.loader import render_to_string
# In a temporary folder, make a temporary file
tmp_folder = mkdtemp()
os.chdir(tmp_folder)        
texfile, texfilename = mkstemp(dir=tmp_folder)
# Pass the TeX template through Django templating engine and into the temp file
os.write(texfile, render_to_string('tex/base.tex', {'var': 'whatever'}))
os.close(texfile)
# Compile the TeX file with PDFLaTeX
call(['pdflatex', texfilename])
# Move resulting PDF to a more permanent location
os.rename(texfilename + '.pdf', dest_folder)
# Remove intermediate files
os.remove(texfilename)
os.remove(texfilename + '.aux')
os.remove(texfilename + '.log')
os.rmdir(tmp_folder)
return os.path.join(dest_folder, texfilename + '.pdf')

The dest_folder variable is usually set to somewhere in the media directory, so that the PDF can then be served statically. The value returned is the path to the file on disk. The logic of what its URL would be is handled by whatever function sets the dest_folder.

dest_folder变量通常设置在media目录中的某个位置,以便可以静态地提供PDF。返回的值是磁盘上文件的路径。它的URL的逻辑由设置dest_folder的任何函数处理。