gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:45:37 65 4
gpt4 key购买 nike

我正在关注 xhtml2pdf guides .

我使用了其中一个示例 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 行 EOL 有关 - 但事实并非如此。

谁能看出我做错了什么?

最佳答案

当我将所有 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>
...

请注意,如果我将它们分开以便每个定义都有一个单独的行,它会挂起。

关于python - 使用 xhtml2pdf pisa.CreatePDF() 创建 pdf 时出现 CSS 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23845947/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com