我在浏览器中的Django中有PISA生产.pdfs,但是如果我想自动将文件写入磁盘怎么办?我要做的是能够在指定的时间点生成.pdf版本文件并将其保存在上传目录中,因此没有浏览器交互。这可能吗?

有帮助吗?

解决方案

对的,这是可能的。例如,使用来自 格雷格·纽曼(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()

您只需要使用模板,dict中的数据和文件名调用write_pdf即可。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top