简体   繁体   English

CssParser错误,同时将html转换为pdf,PISA和Python

[英]CssParser Error, while converting html to pdf ,PISA & Python

Im trying to convert html documents into pdf file using pisa and python. 我试图使用比萨和python将html文档转换为pdf文件。 It works fine for small html code. 它适用于小型html代码。 But when you pass google.com's html data through it , or in fact any big html file , it troughs this error. 但是,当您通过它传递google.com的html数据(或实际上是任何大的html文件)时,都会遇到此错误。

here is the code that converts the html to pdf: 这是将html转换为pdf的代码:

import ho.pisa as pisa
import sys
import os
ls =[]
for arg in sys.argv:
    ls.append(arg)
pisa.showLogging()
print ls

html_file = open(ls[1])
HTML = html_file.read()
filename = os.path.basename(str(ls[1]))
print filename
str(os.getcwd()+filename)
pdfFile =open(str(os.getcwd()+filename), "wb")
pdf = pisa.CreatePDF(HTML,pdfFile)

if not pdf.err:
    print "ds"
    pisa.startViewer(filename)

pdfFile.close()
html_file.close()

and this the error that is thrown : 这是抛出的错误:

ERROR [ho.pisa] C:\Python27\lib\site-packages\sx\pisa3\pisa_document.py line 223: Document error

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_document.py", line 128, in pisaDocument
    c = pisaStory(src, path, link_callback, debug, default_css, xhtml, encoding,
 c=c, xml_output=xml_output)
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_document.py", line 73, in pisaStory
    pisaParser(src, c, default_css, xhtml, encoding, xml_output)
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_parser.py", line 626, in pisaParser
    c.parseCSS()
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_context.py", line 545, in parseCSS
    self.css = self.cssParser.parse(self.cssText)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 358, in parse
    src, stylesheet = self._parseStylesheet(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 453, in _parseStylesheet
    src, atResults = self._parseAtKeyword(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 577, in _parseAtKeyword
    src, result = self._parseAtIdent(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 722, in _parseAtIdent
    src, stylesheet = self._parseStylesheet(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 458, in _parseStylesheet
    src, ruleset = self._parseRuleset(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 737, in _parseRuleset
    src, properties = self._parseDeclarationGroup(src.lstrip())
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 922, in _parseDeclarationGroup
    raise self.ParseError('Declaration group closing \'}\' not found', src, ctxsrc)
CSSParseError: Declaration group closing '}' not found:: (u'{', u'0%{opacity:0}50%{opa')
Traceback (most recent call last):
  File "trypdf.py", line 16, in <module>
    pdf = pisa.CreatePDF(HTML,pdfFile)
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_document.py", line 128, in pisaDocument
    c = pisaStory(src, path, link_callback, debug, default_css, xhtml, encoding,
 c=c, xml_output=xml_output)
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_document.py", line 73, in pisaStory
pisaParser(src, c, default_css, xhtml, encoding, xml_output)
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_parser.py", line 626, in pisaParser
    c.parseCSS()
  File "C:\Python27\lib\site-packages\sx\pisa3\pisa_context.py", line 545, in parseCSS
self.css = self.cssParser.parse(self.cssText)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 358, in parse
src, stylesheet = self._parseStylesheet(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 453, in _parseStylesheet
    src, atResults = self._parseAtKeyword(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 577, in _parseAtKeyword
    src, result = self._parseAtIdent(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 722, in _parseAtIdent
    src, stylesheet = self._parseStylesheet(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 458, in _parseStylesheet
    src, ruleset = self._parseRuleset(src)
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 737, in _parseRuleset
    src, properties = self._parseDeclarationGroup(src.lstrip())
  File "C:\Python27\lib\site-packages\sx\w3c\cssParser.py", line 922, in _parseDeclarationGroup
    raise self.ParseError('Declaration group closing \'}\' not found', src, ctxsrc)
sx.w3c.cssParser.CSSParseError: Declaration group closing '}' not found:: (u'{', u'0%{opacity:0}50%{opa')

xhmlt2pdf is not going to work with all the websites. xhmlt2pdf不适用于所有网站。 Instead, you can use pdfkit : 相反,您可以使用pdfkit

import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')

Edit: I've found another solution with PyQt (from here , thanks to Mark K): 编辑:我发现了PyQt的另一种解决方案(在这里 ,感谢Mark K):

import sys 
from PyQt4.QtCore import *
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 

app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://www.yahoo.com"))
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("fileOK.pdf")

def convertIt():
    web.print_(printer)
    print "Pdf generated"
    QApplication.exit()

QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)
sys.exit(app.exec_())

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

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