0

次の関数を使用して、xhthml2pdf から PDF ファイルを生成しています

def render_to_pdf(template_src, context_dict, filename):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), encoding='UTF-8',  
            dest=result, 
            link_callback=fetch_resources )
    return pdf

次に、次の関数を使用して、pdf オブジェクトのリストを圧縮しようとしています。

def generate_zip(object_list, template):
    result = StringIO.StringIO()
    zipped = zipfile.ZipFile(result, "w")
    for object in object_list:
        zipped.writestr("test.pdf", object)
    zipped.close()
    return result.getvalue()

しかし、次のエラーが表示されます

TypeError: object of type 'pisaContext' has no len()

これにより、圧縮に適したオブジェクトを生成していないと思います。私の質問は、xhtml2pdf を使用して zipfile に適した pd ファイルを生成するにはどうすればよいですか?

4

1 に答える 1

0

文字列(バイト)であるpdf必要があります。たぶん、render_to_pdfの結果からresult.getvalue()を使用する必要がありますか?

私はpisaをあまり使用していません:PisaDocumentのシリアル化メソッドもあります:pdf.dest.getvalue()

于 2012-01-08T14:24:32.763 に答える