使用xhtml2pdf pisa.CreatePDF()创建PDF时出现CSS解析错误

7 投票
3 回答
11047 浏览
提问于 2025-04-18 07:28

我正在按照 xhtml2pdf指南 来操作。

我使用了一个示例的 HTML 文件,并保存为 test.html:

<html>
<head>
<style>
    @page {
        size: a4 portrait;
        @frame header_frame {           # Static Frame
            -pdf-frame-content: header_content;
            left: 50pt; width: 512pt; top: 50pt; height: 40pt;
        }
        @frame content_frame {          # Content Frame
            left: 50pt; width: 512pt; top: 90pt; height: 632pt;
        }
        @frame footer_frame {           # Another static Frame
            -pdf-frame-content: footer_content;
            left: 50pt; width: 512pt; top: 772pt; height: 20pt;
        }
    }
</style>
</head>

<body>
    <!-- Content for Static Frame 'header_frame' -->
    <div id="header_content">Lyrics-R-Us</div>

    <!-- Content for Static Frame 'footer_frame' -->
    <div id="footer_content">(c) - page <pdf:pagenumber>
        of <pdf:pagecount>
    </div>

    <!-- HTML Content -->
    To PDF or not to PDF
</body>
</html>

然后我把这个文件读入为一个字符串,尝试创建一个 PDF:

with open('test.html','r') as f:
    sourceHtml = f.read()
outputFilename = "test.pdf"
resultFile = open(outputFilename , "w+b")
pisaStatus = pisa.CreatePDF(sourceHtml,dest=resultFile)
resultFile.close()

但是,我遇到了以下错误信息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\xhtml2pdf\document.py", line 89, in pisaDocument
    encoding, context=context, xml_output=xml_output)
  File "C:\Python27\lib\site-packages\xhtml2pdf\document.py", line 57, in pisaStory
    pisaParser(src, context, default_css, xhtml, encoding, xml_output)
  File "C:\Python27\lib\site-packages\xhtml2pdf\parser.py", line 660, in pisaParser
    context.parseCSS()
  File "C:\Python27\lib\site-packages\xhtml2pdf\context.py", line 428, in parseCSS
    self.css = self.cssParser.parse(self.cssText)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 431, in parse
    src, stylesheet = self._parseStylesheet(src)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 530, in _parseStylesheet
    src, atResults = self._parseAtKeyword(src)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 650, in _parseAtKeyword
    src, result = self._parseAtPage(src)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 746, in _parseAtPage
    src, atResults = self._parseAtKeyword(src)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 657, in _parseAtKeyword
    src, result = self._parseAtFrame(src)
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 766, in _parseAtFrame
    src, properties = self._parseDeclarationGroup(src.lstrip())
  File "C:\Python27\lib\site-packages\xhtml2pdf\w3c\cssParser.py", line 1017, in _parseDeclarationGroup
    raise self.ParseError('Declaration group closing \'}\' not found', src, ctxsrc)
xhtml2pdf.w3c.cssParser.CSSParseError: Declaration group closing '}' not found:: (u'{           ', u'# Static Frame\n     ')

这个错误似乎表示在 CSS 代码中缺少一个 } 符号。不过我觉得代码看起来没问题。我使用的是 Windows 系统,所以我想这可能和 \r\n 换行符有关,但其实并不是。

有没有人能帮我看看我哪里做错了?

3 个回答

0

你可以在HTML里面使用CSS

 <!DOCTYPE html>
<html lang="en">
  <head>
    <style type="text/css">
      /* Style the header: fixed position (always stay at the top) */
      .header {
        position: fixed;
        top: 0;
        z-index: 1;
        text-align: center;
        width: 100%;
        background-color: #f1f1f1;
      }

      .content {
        font-size: large;
        text-align: center;
        text-emphasis-color: red;
      }
    </style>
  </head>
  <body>
    <div class="header">
    </div>

    <div class="content">
    </div>
  </body>
</html>

外部的CSS或样式表在xhtml2pdf中是不能使用的

5

当我把所有的CSS定义放在一行的时候,它没有出错,可以正常工作:

<html>
<head>
<style>
    @page {size: a4 portrait; @frame header_frame {-pdf-frame-content: header_content; left: 50pt; width: 512pt; top: 50pt; height: 40pt;} @frame content_frame { left: 50pt; width: 512pt; top: 90pt; height: 632pt;} @frame footer_frame { -pdf-frame-content: footer_content; left: 50pt; width: 512pt; top: 772pt; height: 20pt;}}
</style>
</head>
...

但是,如果我把它们分开,每个定义单独一行的话,就会卡住。

5

你遇到的问题和错误没有直接关系,但还是跟你的CSS有点关系。

在CSS里,注释是用 /**/ 来表示的,而不是像Python那样用 #。如果你把文件里的“注释”去掉,pisa就能正常解析和生成你的代码。这也是为什么你那段单行代码能正常工作,因为它们没有被算作注释。

撰写回答