Django 中的 Pisa (XHTML -> PDF) 将不会显示 PDF 中的图像
首先,我已经看到了有关此问题的其他线程,并且我几乎尝试了所有方法来尝试解决此问题,但是...
当使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经有一段时间没有看这个了,但我认为你必须使用 lambda 或 functools。
例如
It's been a while since I looked at this, but I think you have to use lambda or functools.
e.g.
我想这可能会有所帮助。 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