当URL以开头时,XML2PDF引发异常/

2024-04-28 23:01:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个Django HTML模板和一些harcoded 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编写URL时,抛出以下错误:

Exception Type: AttributeError

Exception Value: 'NoneType' object has no attribute 'startswith'

在线

/home/www/env/lib/python2.7/site-packages/xhtml2pdf-0.1b3-py2.7.egg/xhtml2pdf/实用程序.py在init中

^{pr2}$

如果删除“/”则xml2pdf开始工作,但模板无法加载字体文件。 我该怎么做?在


Tags: src模板urlexceptionfontsstaticttffamily
1条回答
网友
1楼 · 发布于 2024-04-28 23:01:28

当我深入研究时,真正的问题最终暴露了它自己是mime类型的问题。在

我检查所有步骤时发现了问题。在一个步骤中,xml2pdf尝试查找文件的mime类型,看起来python2.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)

我将下面的行附加到函数以更正问题:

^{pr2}$

相关问题 更多 >