我在浏览器中让pisa在django中生成.pdfs很好,但是如果我想自动将文件写入磁盘怎么办?我想要做的是能够在指定的时间点生成一个.pdf版本的文件,并将其保存在一个上传目录中,这样就没有了浏览器的交互。这个是可能的吗?
发布于 2010-05-25 04:23:25
是的,这是可能的。例如,使用Greg Newman中的代码作为启动器:
from django.template.loader import get_template
from django.template import Context
import ho.pisa as pisa
import cStringIO as StringIO
import cgi
def write_pdf(template_src, context_dict, filename):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = open(filename, 'wb') # Changed from file to filename
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")), result)
result.close()您只需要使用模板、字典中的数据和文件名来调用write_pdf。
https://stackoverflow.com/questions/2899807
复制相似问题