【问题标题】:XML2PDF Throwing Exception while URL starts with /当 URL 以 / 开头时 XML2PDF 抛出异常
【发布时间】:2017-09-07 09:24:04
【问题描述】:

我得到了一个带有一些硬编码 CSS 部分的 Django HTML 模板。

@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 中编写带有“/”前缀的 URL 时,xml2pdf 在下面抛出此错误:

异常类型:属性错误

异常值:“NoneType”对象没有“startswith”属性

一行

/home/www/env/lib/python2.7/site-packages/xhtml2pdf-0.1b3-py2.7.egg/xhtml2pdf/util.py in init

line: 621. if self.mimetype.startswith('text'): ...

如果我删除“/”,则 xml2pdf 开始工作,但模板无法加载字体文件。 我怎样才能做到这一点?

【问题讨论】:

    标签: python css xml django pdf


    【解决方案1】:

    当我深入研究时,真正的问题终于揭示了它本身就是 mime 类型的问题。

    我在检查所有步骤时发现了问题。在一个步骤中,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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2017-11-26
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多