我有一个Django HTML模板,里面有一些带有编码的CSS部件。
@font-face {
font-family:"Arial";
src: url(/static/fonts/Arial.ttf);
}
@font-face {
font-family:"Arial_Black";
src: url(/static/fonts/Arial_Black.ttf);
}
当我在CSS中写下网址' /' xml2pdf在下面抛出此错误:
异常类型:AttributeError
例外值:' NoneType'对象没有属性' startswith'
在第
行 init /home/www/env/lib/python2.7/site-packages/xhtml2pdf-0.1b3-py2.7.egg/xhtml2pdf/util.pyline: 621. if self.mimetype.startswith('text'): ...
如果我删除' /'然后xml2pdf开始工作,但模板无法加载字体文件。 我怎样才能做到这一点?
答案 0 :(得分:1)
当我深入研究它时,真正的问题终于揭示了它作为哑剧类型的问题。
当我检查所有步骤时,我发现了问题。在一个步骤中,xml2pdf尝试查找该文件的mime类型,看起来Python 2.7.6没有 .ttf 文件的mime类型描述
>>> from mimetypes import MimeTypes
>>> import urllib
>>> url = urllib.pathname2url('static/fonts/Arial.ttf')
>>> mime = MimeTypes()
>>> mime_type = mime.guess_type(url)
>>> print mime_type
(None, None)
>>> url = urllib.pathname2url('wsgi.py')
>>> mime_type = mime.guess_type(url)
>>> print mime_type
('text/x-python', None)
我将下面的行附加到功能以纠正问题:
import mimetypes
mimetypes.add_type('application/font-ttf', '.ttf')