django - pisa : adding images to PDF output

前端 未结 7 1015
醉酒成梦
醉酒成梦 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:16

    All the above code did not worked for me. In the end i got it working by putting the get_full_path procedure. So the final code looks like this

    def render_to_pdf( template_src, context_dict):
        now = datetime.now()
        filename = now.strftime('%Y-%m-%d') + '.pdf'
        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")),result, path=path)
    
        if not pdf.err:
          response = HttpResponse(result.getvalue(), mimetype='application/pdf')
          response['Content-Disposition'] = 'attachment; filename="'+filename+'"'
          return response
       return HttpResponse('We had some errors
    %s
    ' % escape(html)) def get_full_path_x(request): full_path = ('http', ('', 's')[request.is_secure()], '://', request.META['HTTP_HOST'], request.path) return ''.join(full_path)

提交回复
热议问题