9

ブラウザのdjangoで.pdfを生成するpisaを使用していますが、ファイルを自動的にディスクに書き込みたい場合はどうすればよいですか?私がやりたいのは、指定された時点で.pdfバージョンのファイルを生成し、それをアップロードディレクトリに保存できるようにすることです。これにより、ブラウザの操作は不要になります。これは可能ですか?

4

1 に答える 1

15

はい、可能です。たとえば、GregNewmanのコードをスターターとして使用します

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を呼び出す必要があります。

于 2010-05-24T20:23:25.017 に答える