我有一个 html 字符串,我想在 python 中存储为 PDF 文件。我为此目的使用PDFkit 。下面是我为此尝试的代码。在下面的代码中,我还尝试通过龙卷风服务器提供图像。
class MainHandler(RequestHandler):
def get(self):
self.write('hello!')
class ImageHandler(RequestHandler):
def get(self):
d={}
d["mov1"]=1
d["mov2"]=10
d["mov3"]=40
d["mov4"]=3
py.bar(range(len(d)),d.values(),align="center")
py.xticks(range(len(d)),d.keys())
io=StringIO()
py.savefig(io,format='svg')
self.set_header("Content-Type", "image/svg+xml")
print io.getvalue()
config = pdfkit.configuration(wkhtmltopdf='E:\\wkhtmltopdf\\bin')
pdfkit.from_string(io.getvalue(),"E:\\hello.pdf",configuration=config) #Error here
self.write(io.getvalue())
app = Application([
url(r"/", MainHandler),
url(r"/Image",ImageHandler)
])
if __name__=="__main__":
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()
我已经在 E 盘安装了 wkhtmltopdf。我遇到了一个例外,
ERROR:tornado.application:Uncaught exception GET /Image (::1)
HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/Image', version='HTTP/1.1', remote_ip='::1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Host': 'localhost:8888', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', 'Connection': 'keep-alive', 'Cookie': '_ga=GA1.1.359367893.1418721747', 'If-None-Match': '"ee884f005691a9736e5e380cc68cd4c9679bf2a7"'})
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado\web.py", line 1332, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "E:\eclipse_workspace\Visualisation\module1\mod1.py", line 61, in get
config = pdfkit.configuration(wkhtmltopdf='E:\\wkhtmltopdf\\bin')
File "C:\Python27\lib\site-packages\pdfkit\api.py", line 79, in configuration
return Configuration(**kwargs)
File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: "E:\wkhtmltopdf\bin"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
ERROR:tornado.access:500 GET /Image (::1) 246.00ms
虽然我愿意使用任何其他软件包。我也想知道我做错了什么。