zipping up xhtml2pdf pdf's with zipfile

474 views Asked by At

I'm using the following function to generate my pdf files from xhthml2pdf

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

Next, I'm using the following function in an attempt to zip up a list of pdf objects

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()

But I get the following error

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

Which leads me to believe that I am not generating the right object for zipping. So my question is, how do I go about using xhtml2pdf to generate a pd file fit for zipfile?

1

There are 1 answers

0
Roman Susi On

The pdf must be a string (bytes). Maybe, you need to use result.getvalue() from the render_to_pdf's result?

I have not used pisa extensively: there is a serialization method for PisaDocument as well: pdf.dest.getvalue()