2014-05-24 5 views
1

xhtml2pdfguides을 따르고 있습니다.xhtml2pdf로 PDF를 만들 때 CSS 구문 분석 오류가 발생합니다. pisa.CreatePDF()

내가 샘플 HTML 파일 중 하나를 사용 test.html를로 저장 한 : 나는 다음 문자열로이 파일에 읽고 PDF로 만들려고

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

:

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 코드에서. 그러나 그것은 나에게 좋게 보인다. 그래서 나는 그것이 \r\n 라인 EOL과 관련이있을 것이라고 생각했기 때문에 창을 사용하고있다.

내가 뭘 잘못하고 있는지 누가 알 수 있습니까? 내가 한 줄에 모든 CSS 정의를 넣을 때

+0

예제에는 사용자가있는 한 줄에 여러 CSS 속성 정의가 없습니다. * 문제가된다면 실망 스럽겠지만 여전히 시도해 볼 가치가 있습니다. – usr2564301

+0

@Jongware 그 반대인가? 모든 CSS 정의를 한 줄에 넣으면 주석이 제거 된'@page {...} '가 작동합니다. 어느 쪽이든, 나는 그것을 조사하도록 고무시켜 주셔서 감사합니다 :-). 원한다면 답변으로 게시하십시오. – atomh33ls

+0

그 반대입니까? 너는'왼쪽 : 50pt; 너비 : 512pt; .. ", 예제는 그렇지 않습니다. 이것이 실제로 문제 였다면, 그것은 추측 일 뿐이므로 답변으로 게시하십시오. 이 표기법이 실제로 치명적이지 않아야하기 때문에'xhtml2pdf'에 대한 버그를 제기 할 수도 있습니다. – usr2564301

답변

1

당신이 타격을 가하는 문제는 오류와 완전히 관련이 없지만 여전히 CSS가 관련되어 있습니다.

/**/을 사용하여 작성한 코드입니다 (파이썬에서는 #이 아닙니다). 파일에서 "주석"을 제거하면 pisa가 구문 분석하여 작성합니다. 이것은 한 줄 예제가 포함 된 이유이기도하며 포함되지 않았습니다.

+0

github의 예제 CSS가 작동하지 않는지 확인할 수 있습니까? – usr2564301

+0

감사합니다. [github의 usage.rst 파일] (https://github.com/4c656554/xhtml2pdf/commit/1b51be88eba2998e25644f07e8bc681ad7134b38)에 대한 업데이트를 제안했습니다. – atomh33ls

1

, 그것은 오류없이 작동합니다

<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> 
... 

주, 나는 각 정의는 별도의 라인을 가지고 그래서 그들을 분리하는 경우, 그것은 중단됩니다.

+0

실제로 나는 내가 추정 한 *과 그 오류의 반대이며, 가장 확실하게 온라인 예제에 표시된 것이 아닙니다. 이 문제에 대해 버그를 제기 할 수 있습니다. – usr2564301

+1

[완료] (https://github.com/chrisglass/xhtml2pdf/issues/188). – atomh33ls

관련 문제