为Django配置Pisa应该并不难。
网上确实有几个示例向您展示了如何执行此操作,并说明了如何链接到模板中的外部资源:
- http://www.arnebrodowski.de/blog/501-Pisa-and-Reportlab-pitfalls.html
- django-比萨:将图像添加到PDF输出
- http://antydba.blogspot.com/2009/12/django-pisa-polskie-czcionki.html
- http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html
在您的情况下,您应该尝试第一篇博文中提到的链接回调函数:
def fetch_resources(uri, rel): """ Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc. `uri` is the href attribute from the html link element. `rel` gives a relative path, but it's not used here. """ path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, "")) return path
对于较新的Django版本,您可能应该使用
STATIC_ROOT而不是
MEDIA_ROOT
然后
fetch resources在渲染方法中相应地使用:
pdf = pisa.pisadocument(StringIO.StringIO( html.enpre("UTF-8")), result, link_callback=fetch_resources, encoding="utf-8")


