質問
ブラウザでdjangoで.pdfsを生産しているpisaは問題ありませんが、ファイルを自動的にディスクに書き込みたい場合はどうなりますか?私がやりたいのは、指定されたポイントで.pdfバージョンファイルを生成し、アップロードディレクトリに保存できるため、ブラウザの相互作用はありません。これは可能ですか?
解決
はい、それは可能です。たとえば、からのコードを使用します グレッグ・ニューマン スターターとして:
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を呼び出すだけです。
所属していません StackOverflow