简体   繁体   中英

Unable to load images/files pisa pdf Django python

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 ie /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.

我终于解决了我遇到的问题...事实证明,如果我使用css设置body的高度,那将是行不通的...一旦我删除了那条线,图像就可以完美加载了...

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.

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

<img src="/static/images/<name>.jpg" />

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM