简体   繁体   English

无法加载图像/文件比萨pdf Django python

[英]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. 我遇到了一个问题,即使我指定@font-face使用UTF-8字体,它也不会显示中文字符。 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. 原来我也无法显示图像...所以我似乎无法将任何文件嵌入到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 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. 我看了关于SO的其他任务,但这没有帮助。 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. 同样在fetch_resources函数中,返回的路径是文件的正确完整路径,即/home/<user>/project/static/images/bc_logo_bw_pdf.png/home/<user>/project/static/fonts/wingdings.ttf和我不知所措。

UPDATE Everytime I create a pdf, I get this message on the console 更新每次创建pdf时,都会在控制台上收到此消息

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

could this be related? 这可能相关吗?

UPDATE #2 更新#2

The font works now I made a dumb mistake... The font I was using did not have the Chinese unicode. 字体有效,现在我犯了一个愚蠢的错误...我使用的字体没有中文unicode。 But I still cannot embed any images onto the pdf, be it jpeg, gif or png. 但是我仍然无法在pdf上嵌入任何图像,无论是jpeg,gif还是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. 对我来说(django 1.4,python 2.7 pisa == 3.0.33),如果我放置图像的完整路径而不是相对路径,它对我有用。 Try doing the same. 尝试做同样的事情。

Everything looks better . 一切看起来都更好。 Try once with JPG image file. 尝试使用JPG图像文件。 In my case PNG file was also not working. 就我而言,PNG文件也不起作用。

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

without width and height attribute image will not work. 没有widthheight属性的图片将无法工作。 add width and height attribute. 添加width和height属性。

<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. 不要放弃XHTML2PDF Pisa。
Pisa use PIL for generate PDF and use lib zip decoder to inserting images. 比萨使用PIL生成PDF,并使用lib zip解码器插入图像。
You should check if your PIL already installed properly with zip decoder, fonts and several components 您应该检查您的PIL是否已经正确安装了zip解码器,字体和一些组件

I have solve this problem by installing PIL with zip decoder. 我已经通过安装带有zip解码器的PIL解决了这个问题。
http://obroll.com/install-python-pil-python-image-library-on-ubuntu-11-10-oneiric/ 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/ 如果您需要更多详细信息,可以在这里阅读我的文章: http : //obroll.com/how-to-load-images-files-style-css-in-pdf-using-pisa-xhtml2pdf-on-django/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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