django - pisa : adding images to PDF output

前端 未结 7 1009
醉酒成梦
醉酒成梦 2020-11-28 06:50

I\'m using a standard example from the web (http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html) to convert a django view / template into a PDF.

7条回答
  •  [愿得一人]
    2020-11-28 07:11

    def render_to_pdf( template_src, context_dict):
    
        template = get_template(template_src)
        context = Context(context_dict)
        html  = template.render(context)
        result = StringIO.StringIO()
    
        if page has an image.something:
            pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources)
        else  no image.something :
            pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),result)
    
        if not pdf.err:
            return HttpResponse(result.getvalue(), mimetype='examination_report/pdf')
        return HttpResponse('We had some errors
    %s
    ' % escape(html)) def fetch_resources(uri, rel): if os.sep == '\\': # deal with windows and wrong slashes uri2 = os.sep.join(uri.split('/')) else:# else, just add the untouched path. uri2 = uri path = '%s%s' % (settings.SITE_ROOT, uri2) return path

提交回复
热议问题