Unable to load images/files pisa pdf Django python

7.4k views Asked by At

I had a problem before where it wouldn't show Chinese characters even when I specified @font-face to use a UTF-8 font. It turns out I cannot display images as well... so I seems like I am unable to get any of the files embeded into my pdf.

This is the code I use:

def render_to_pdf(template_src, context_dict):
    """Function to render html template into a pdf file"""
    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")),
                                            dest=result,
                                            encoding='UTF-8',
                                            link_callback=fetch_resources)
    if not pdf.err:
        response = http.HttpResponse(result.getvalue(), mimetype='application/pdf')

        return response

    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

def fetch_resources(uri, rel):
    import os.path
    from django.conf import settings
    path = os.path.join(
            settings.STATIC_ROOT,
            uri.replace(settings.STATIC_URL, ""))
    return path

html

<img src="/static/images/bc_logo_bw_pdf.png" />

and

    @font-face {
        font-family: "Wingdings";
        src: url("/static/fonts/wingdings.ttf");
    }

I looked at the other quests on SO but it was no help. There are also no exceptions happening in the two functions. Also in fetch_resources function the path returned was the correct full path to the file i.e. /home/<user>/project/static/images/bc_logo_bw_pdf.png and /home/<user>/project/static/fonts/wingdings.ttf and I am at a loss as to what is wrong.

UPDATE Everytime I create a pdf, I get this message on the console

No handlers could be found for logger "ho.pisa"

could this be related?

UPDATE #2

The font works now I made a dumb mistake... The font I was using did not have the Chinese unicode. But I still cannot embed any images onto the pdf, be it jpeg, gif or png.

5

There are 5 answers

0
Anil Arya On

Everything looks better . Try once with JPG image file. In my case PNG file was also not working.

<img src="/static/images/<name>.jpg" />
0
SAFEER N On

without width and height attribute image will not work. add width and height attribute.

<img src="{% static 'images/logo.png' %}" alt="image" width="200" height="150" />

this fix works for me.

0
yodi On

I have the same problem here. Don't give up with XHTML2PDF Pisa.
Pisa use PIL for generate PDF and use lib zip decoder to inserting images.
You should check if your PIL already installed properly with zip decoder, fonts and several components

I have solve this problem by installing PIL with zip decoder.
http://obroll.com/install-python-pil-python-image-library-on-ubuntu-11-10-oneiric/

If you need more detail information, you can read my article here : http://obroll.com/how-to-load-images-files-style-css-in-pdf-using-pisa-xhtml2pdf-on-django/

0
zentenk On

I have finally solved the problem I was having... it turns out it doesn't work if I set the body's height with css... once I removed that line the image was loading perfectly...

0
Vinay Chittora On

For me (django 1.4, python 2.7 pisa==3.0.33), If I put the full path of image instead of relative, it works for me. Try doing the same.