1

我正在尝试使用 xhtml2pdf 从 html 页面创建一个 pdf。在转换它时,我的终端显示以下错误消息。

错误 TypeError: 'NotImplementedType' object is not iterable 是什么意思?

我怎样才能防止这种情况?以下示例代码正在使用....来转换 pdf

 <html>
<head>
<style type="text/css">
   @page 
    {
     @top-right 
     {
      content: "Page " counter(page);
     }
    }
</style>
</head>
<body>
test
</body>
</html>

并且还使用以下命令行进行pdf转换..

xhtml2pdf test.html test.pdf

正如我早先所说的那样,在那次转换中我遇到了一个错误。我能做些什么呢?

4

1 回答 1

0

这看起来是 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 元组,其中包含srcNotImplemented对于它无法识别的任何 CSS at 关键字,特别是对于@top-right. 这会导致extend下一行中的 失败,因为NotImplemented它不是extend.

@top-right我不熟悉,我找不到任何 Google 搜索结果css @top-right。如果我替换@top-right@topright,我会得到相同的行为,如果我替换@top-righttop-right,代码似乎进入了无限循环。所以我对 xhtml2pdf 的 CSS 解析器并没有特别的印象。

将第 757 行替换为

            if atResults is not None and atResults is not NotImplemented:

似乎让 xhtml2pdf 工作得足以将您的测试文档转换为 PDF。但是,它生成的输出可能不是您所希望的。

我怀疑最好的办法是在xhtml2pdf 邮件列表上发帖。

于 2014-08-08T12:39:30.343 回答