2010-05-24 63 views
7

我在浏览器中的django中生成.pdf的罚款,但如果我想自动将文件写入磁盘?我想要做的是能够在指定的时间点生成.pdf版本文件并将其保存在上传目录中,因此不存在浏览器交互。这可能吗?使用比萨写入pdf到磁盘

回答

12

是的,这是可能的。例如,使用代码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一个模板,在一个字典和文件名的数据。

+0

谢谢 - 正是我所需要的。 – PhoebeB 2010-05-24 20:30:11