简体   繁体   English

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

[英]CSS parsing error when creating pdf with xhtml2pdf pisa.CreatePDF()

I am following the xhtml2pdf guides .我正在关注xhtml2pdf 指南

I have used one of the sample html files and saved as test.html:我使用了示例 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>

I then read in this file as a string and attempt to create a pdf:然后我将此文件作为字符串读入并尝试创建一个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()

However, I get the following error traceback:但是,我收到以下错误回溯:

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     ')

This seems to imply that there is a missing } in the css code.这似乎暗示css代码中缺少} However it seems fine to me.不过我觉得还好。 I am using windows so I thought it might be to do with \\r\\n line EOLs - but it isn't.我正在使用 Windows,所以我认为这可能与\\r\\n行 EOL 有关 - 但事实并非如此。

Can anyone see what I'm doing wrong?谁能看到我做错了什么?

When I put all the CSS definitions in a single line, it works without error: 当我将所有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>
...

Note, if I separate them out so each definition has a separate line, it hangs. 请注意,如果我将它们分开,以便每个定义都有一个单独的行,它就会挂起。

The problem you are hitting is completely unrelated to the error, but still involves your CSS. 您遇到的问题与错误完全无关,但仍然涉及到您的CSS。

Comments in CSS are created using /* and */ , not # like in Python. 在CSS注释使用创建/**/ ,而不是#在Python等等。 If you remove the "comments" from your file, it is parsed and created by pisa. 如果从文件中删除“注释”,则由pisa解析并创建。 This is also why your single line example worked, they weren't included. 这也是您的单行示例有效的原因,但未包括在内。

You can apply CSS inside HTML您可以在 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>

External CSS/Stylesheets are not applicable to xhtml2pdf外部 CSS/样式表不适用于 xhtml2pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM