您(和其他人)面临的问题是 PDF 无法直接在浏览器中显示。
获得类似内容的唯一可能方法是使用图像转换器从 PDF 中创建 PNG 或 JPG 并显示此文件。
这可以通过 imagemagick 和自定义显示功能来完成。
更新 1
一个简单的解决方案是使用 wand (http://docs.wand-py.org) 一个 python-imagemagick 绑定。我试过 Ubuntu 13.04:
文本形式:
from wand.image import Image as WImage
img = WImage(filename='hat.pdf')
img
对于多页 pdf,您可以获得例如第二页通过:
img = WImage(filename='hat.pdf[1]')
更新 2
由于最近的浏览器支持使用其嵌入式 pdf 查看器显示 pdf,因此可以将基于 iframe 的替代解决方案实现为
class PDF(object):
def __init__(self, pdf, size=(200,200)):
self.pdf = pdf
self.size = size
def _repr_html_(self):
return '<iframe src={0} width={1[0]} height={1[1]}></iframe>'.format(self.pdf, self.size)
def _repr_latex_(self):
return r'\includegraphics[width=1.0\textwidth]{{{0}}}'.format(self.pdf)
这个类实现了 html 和 Latex 的表示,因此 pdf 也可以在 nbconversion 到 Latex 后继续存在。可以这样使用
PDF('hat.pdf',size=(300,250))
对于 Firefox 33,这会导致