Python 自动生成PDF
什么是生成PDF报告的可靠工具?特别是,我们对创建包含视频的交互式PDF感兴趣,如示例所示 现在我们正在使用Python和生成PDF,但还没有完全研究该库(主要是因为许可证定价有点高) 我们一直在研究Adobe和库,但很难说它们的功能是什么 能够从PDF模板生成文档将是一个加号 如有任何提示或意见,将不胜感激Python 自动生成PDF,python,pdf,adobe,report,acrobat,Python,Pdf,Adobe,Report,Acrobat,什么是生成PDF报告的可靠工具?特别是,我们对创建包含视频的交互式PDF感兴趣,如示例所示 现在我们正在使用Python和生成PDF,但还没有完全研究该库(主要是因为许可证定价有点高) 我们一直在研究Adobe和库,但很难说它们的功能是什么 能够从PDF模板生成文档将是一个加号 如有任何提示或意见,将不胜感激 谢谢,最近,我需要为Django应用程序创建PDF报告;ReportLab许可证是可用的,但我最终选择了LaTeX。这种方法的好处是,我们可以使用它来生成LaTeX源代码,而不必为需要创建
谢谢,最近,我需要为Django应用程序创建PDF报告;ReportLab许可证是可用的,但我最终选择了LaTeX。这种方法的好处是,我们可以使用它来生成LaTeX源代码,而不必为需要创建的许多报告编写大量代码。此外,我们还可以利用相对更简洁的LaTeX语法(它确实有许多怪癖,并不适合所有用途) 提供该方法的一般概述。我发现有必要做一些修改,我在问题的末尾已经提供了这些修改。主要的附加功能是检测
重新运行LaTeX
消息,这表明需要额外的过程。用法非常简单,如下所示:
def my_view(request):
pdf_stream = process_latex(
'latex_template.tex',
context=RequestContext(request, {'context_obj': context_obj})
)
return HttpResponse(pdf_stream, content_type='application/pdf')
可以在LaTeX生成的PDF中嵌入视频,但是我没有任何经验。这是一件上衣
此解决方案确实需要生成一个新进程(pdflatex
),因此如果您想要纯Python解决方案,请继续查找
import os
from subprocess import Popen, PIPE
from tempfile import NamedTemporaryFile
from django.template import loader, Context
class LaTeXException(Exception):
pass
def process_latex(template, context={}, type='pdf', outfile=None):
"""
Processes a template as a LaTeX source file.
Output is either being returned or stored in outfile.
At the moment only pdf output is supported.
"""
t = loader.get_template(template)
c = Context(context)
r = t.render(c)
tex = NamedTemporaryFile()
tex.write(r)
tex.flush()
base = tex.name
names = dict((x, '%s.%s' % (base, x)) for x in (
'log', 'aux', 'pdf', 'dvi', 'png'))
output = names[type]
stdout = None
if type == 'pdf' or type == 'dvi':
stdout = pdflatex(base, type)
elif type == 'png':
stdout = pdflatex(base, 'dvi')
out, err = Popen(
['dvipng', '-bg', '-transparent', names['dvi'], '-o', names['png']],
cwd=os.path.dirname(base), stdout=PIPE, stderr=PIPE
).communicate()
os.remove(names['log'])
os.remove(names['aux'])
# pdflatex appears to ALWAYS return 1, never returning 0 on success, at
# least on the version installed from the Ubuntu apt repository.
# so instead of relying on the return code to determine if it failed,
# check if it successfully created the pdf on disk.
if not os.path.exists(output):
details = '*** pdflatex output: ***\n%s\n*** LaTeX source: ***\n%s' % (
stdout, r)
raise LaTeXException(details)
if not outfile:
o = file(output).read()
os.remove(output)
return o
else:
os.rename(output, outfile)
def pdflatex(file, type='pdf'):
out, err = Popen(
['pdflatex', '-interaction=nonstopmode', '-output-format', type, file],
cwd=os.path.dirname(file), stdout=PIPE, stderr=PIPE
).communicate()
# If the output tells us to rerun, do it by recursing over ourself.
if 'Rerun LaTeX.' in out:
return pdflatex(file, type)
else:
return out
我建议使用将HTML呈现为PDF
并使用您选择的任何工具将报告呈现为HTML。我喜欢不确定它是否可以生成pdf格式的视频。可能是html5视频标签。但可能不是。我使用HTML到PDF渲染器(特别是wkhtmltopdf)的经验是,与使用“核心”PDF库(如reportlab)生成的PDF文件相比,生成的PDF文件质量不是很好。出于好奇:为什么选择LaTeX而不是reportlab的RML?@Goro:我直到现在才知道RML的存在。谢谢。:)