Django中的比萨(XHTML - > PDF)将不会以PDF格式显示图像 [英] Pisa (XHTML -> PDF) in Django will not display images in PDF

查看:162
本文介绍了Django中的比萨(XHTML - > PDF)将不会以PDF格式显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始,我已经看到其他线程了,我已经尝试了几乎所有的东西来尝试解决这个问题,但是...

To start, I've seen the other threads on this, and I have tried nearly everything to try to fix this but...

当使用比萨将HTML页面呈现为PDF,HTML中的图像显然缺少。也就是说,当页面呈现为HTML时,一切都只是花花公子,但是当我使用比萨将输出切换为PDF时,图像消失。

When using Pisa to render an HTML page to PDF, the images in said HTML go conspicuously missing. That is to say, when the page is rendered to HTML, everything is just dandy, but when I switch the output to PDF using Pisa, the images disappear.

最多常见的事情是创建一个链接回调函数:

The most common thing I've found to do is to create a link callback function thus:

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

def my_view(request, variable1):
    html = render_to_string('template_goes_here.html', dict, context_instance=RequestContext(request))
    result = StringIO()
    pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources)
    if not pdf.err:
        return HttpResponse(result.getvalue(), mimetype='application/pdf')
    return HttpResponse('Pisa hates you! %s' % cgi.escape(html))

不行。该PDF出来了,但是图像不存在。

However, this does not work. The PDF comes up, great, but the images are absent.

我还在另一个线程上看到关于将HTML写入mkstemp的建议,通过命令将其转换为PDF线,然后输出HTML,没有成功。

I also read a suggestion on another thread about writing the HTML to mkstemp, converting it to a PDF via command line, and then outputing the HTML, no success there either.

我甚至尝试安装PIL 1.1.16而不是1.1.17,因为有类似问题的人 - 没有骰子。

I even tried installing PIL 1.1.16 instead of 1.1.17 because of someone with a similar issue--no dice.

有没有人想知道我在哪里出错?

Does anyone have an idea on where I'm going wrong here?

推荐答案

这是一段时间,因为我看了这个,但我想你必须使用lambda或functools。

It's been a while since I looked at this, but I think you have to use lambda or functools.

eg

links = lambda uri, rel: os.path.join(settings.MEDIA_ROOT, 
    uri.replace(settings.MEDIA_URL, ''))


pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")),
    dest=result, link_callback=links)

这篇关于Django中的比萨(XHTML - > PDF)将不会以PDF格式显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆