这看起来是 xhtml2pdf 中的一个错误。
运行命令行时得到的回溯如下:
Traceback (most recent call last):
File "C:\Python27\Scripts\xhtml2pdf-script.py", line 9, in <module>
load_entry_point('xhtml2pdf==0.0.6', 'console_scripts', 'xhtml2pdf')()
File "build\bdist.win32\egg\xhtml2pdf\pisa.py", line 146, in command
File "build\bdist.win32\egg\xhtml2pdf\pisa.py", line 363, in execute
File "build\bdist.win32\egg\xhtml2pdf\document.py", line 89, in pisaDocument
File "build\bdist.win32\egg\xhtml2pdf\document.py", line 57, in pisaStory
File "build\bdist.win32\egg\xhtml2pdf\parser.py", line 685, in pisaParser
File "build\bdist.win32\egg\xhtml2pdf\context.py", line 498, in parseCSS
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 434, in parse
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 533, in _parseStylesheet
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 654, in _parseAtKeyword
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 758, in _parseAtPage
TypeError: 'NotImplementedType' object is not iterable
深入研究代码,似乎问题就在这里,在w3c/cssParser.py
if src.startswith('@'):
# @media, @page, @font-face
src, atResults = self._parseAtKeyword(src)
if atResults is not None:
stylesheetElements.extend(atResults) # this is line 758
_parseAtKeyword(src)
返回一个 2 元组,其中包含src
和NotImplemented
对于它无法识别的任何 CSS at 关键字,特别是对于@top-right
. 这会导致extend
下一行中的 失败,因为NotImplemented
它不是extend
.
@top-right
我不熟悉,我找不到任何 Google 搜索结果css @top-right
。如果我替换@top-right
为@topright
,我会得到相同的行为,如果我替换@top-right
为top-right
,代码似乎进入了无限循环。所以我对 xhtml2pdf 的 CSS 解析器并没有特别的印象。
将第 757 行替换为
if atResults is not None and atResults is not NotImplemented:
似乎让 xhtml2pdf 工作得足以将您的测试文档转换为 PDF。但是,它生成的输出可能不是您所希望的。
我怀疑最好的办法是在xhtml2pdf 邮件列表上发帖。