首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Pisa将pdf写入磁盘

使用Pisa将pdf写入磁盘
EN

Stack Overflow用户
提问于 2010-05-25 03:49:06
回答 1查看 5.3K关注 0票数 9

我在浏览器中让pisa在django中生成.pdfs很好,但是如果我想自动将文件写入磁盘怎么办?我想要做的是能够在指定的时间点生成一个.pdf版本的文件,并将其保存在一个上传目录中,这样就没有了浏览器的交互。这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-25 04:23:25

是的,这是可能的。例如,使用Greg Newman中的代码作为启动器:

代码语言:javascript
复制
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。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2899807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档