使用比萨将pdf写入磁盘 [英] Using Pisa to write a pdf to disk
本文介绍了使用比萨将pdf写入磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我让比萨在浏览器中以django格式生成.pdfs,但是如果我想自动将文件写入磁盘该怎么办?我想要做的是能够在指定的时间点生成.pdf版本文件并将其保存在上载目录中,因此没有浏览器交互。
I have pisa producing .pdfs in django in the browser fine, but what if I want to automatically write the file to disk? What I want to do is to be able to generate a .pdf version file at specified points in time and save it in a uploads directory, so there is no browser interaction. Is this possible?
推荐答案
是可以的。例如,使用 Greg Newman 作为启动器:
Yes it is possible. for example, using code from Greg Newman as a starter:
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。
You just need to call write_pdf with a template, data in a dict and a file name.
这篇关于使用比萨将pdf写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文