2

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!

Community
  • 1
  • 1
Quan
  • 473
  • 7
  • 16
  • The PDF file can be generated normally except the image is missing. (I edited my question as well) – Quan Nov 02 '11 at 08:55
  • Issue was fixed. Reason is an error occurs when fetch_resources was executed although no error log can be found. It's only possible to find out by using debugger with a break point set inside that function. – Quan Nov 02 '11 at 12:38
  • Hi Quan. Thanks to share answer with us. Two questions: what's your IDE? Are you confortable with xhtml2pdf? Just curiousity. – dani herrera Nov 02 '11 at 21:24
  • Hi danihp. I'm using Aptana as IDE, it's base on Eclipse and free too, that's a very cool IDE. xhtml2pdf is very easy to use, we can make a report in 2 steps, first is create a template (like django template), second is write a function to get data from db or do something (like django views) and that's all. – Quan Nov 04 '11 at 16:47
  • @Quan, thanks about your comments. I have tested xhtml2pdf and is easy but I have no control over page number ;( . regards – dani herrera Jul 10 '12 at 10:13
  • @Quan Please share with us how you fixed this – Simon Kagwi Oct 24 '12 at 14:17
  • @SimonKagwi I worked on this project that was so long ago, so I don't remember how to fix it anymore. But I'm pretty sure the problem occurred in fetch_resources function. Maybe I didn't change my code, just my settings error. Anyway, try to put some breakpoints to find out the code line where your error occurred, then. – Quan Nov 02 '12 at 05:59

0 Answers0