I have a html string which I want to store as a PDF file in python. I am using PDFkit for that purpose. Below is the code which I tried for the purpose. In the below code I am also trying to serve the image via tornado server.
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()
I have installed wkhtmltopdf in E drive. I am getting an exception,
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
While I am open to using any other packages. I also want to know what is the mistake I am doing.
This link says that given the above error the path should be set to location of wkhtmltopdf binary. Which I thought I did. But it worked for me when the changed the config to