Django 中的 Pisa (XHTML -> PDF) 将不会显示 PDF 中的图像

发布于 2024-12-02 21:10:04 字数 968 浏览 0 评论 0原文

首先,我已经看到了有关此问题的其他线程,并且我几乎尝试了所有方法来尝试解决此问题,但是...

当使用 Pisa 将 HTML 页面渲染为 PDF 时,所述 HTML 中的图像明显丢失。也就是说,当页面呈现为 HTML 时,一切都很漂亮,但是当我使用 Pisa 将输出切换为 PDF 时,图像消失了。

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

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 出来了,很好,但是没有图像。

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

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

有谁知道我在这里出错的地方吗?

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

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))

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-12-09 21:10:05

我已经有一段时间没有看这个了,但我认为你必须使用 lambda 或 functools。

例如

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)

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

e.g.

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)
贱人配狗天长地久 2024-12-09 21:10:05

我想这可能会有所帮助。 Windows 使用斜线是我的情况的罪魁祸首。 https://stackoverflow.com/a/12644633/1236599

I think this might help. Windows use of slashes was the culprit in my case. https://stackoverflow.com/a/12644633/1236599

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文