Output image to pdf with xhtml2pdf

与世无争的帅哥 提交于 2019-12-06 12:04:30

问题


I followed this post : django - pisa : adding images to PDF output and using fetch_resources. The PDF file can be generated normally except the image is missing.

This is my code:

def fetch_resources(uri, rel):
    path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
    return path

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("ISO-8859-1")), 
                            dest=result, 
                            link_callback=fetch_resources )
    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<pre>%s</pre>' % escape(html))

This is my template:

<p><img src="{{ copyright.digital_signature.url }}"></p>

I'm sure that "copyright.digital_signature.url" is correct because it can show an image on browser properly. And that means PIL had been installed?

Can anybody help me what the problem is? Thank you very much!

来源:https://stackoverflow.com/questions/7977673/output-image-to-pdf-with-xhtml2pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!