首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将html字符串保存为python中的PDF

将html字符串保存为python中的PDF
EN

Stack Overflow用户
提问于 2014-12-16 23:39:05
回答 1查看 2.8K关注 0票数 2

我有一个html字符串,我想将其存储为python中的PDF文件。我正在使用PDFkit来达到这个目的。下面是我尝试过的代码。在下面的代码中,我也尝试通过tornado服务器提供图像。

代码语言:javascript
复制
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。我得到了一个例外,

代码语言:javascript
复制
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

而我可以使用任何其他的包。我还想知道我正在做的错误是什么。

EN

回答 1

Stack Overflow用户

发布于 2014-12-17 02:23:29

This链接指出,鉴于上述错误,路径应设置为wkhtmltopdf二进制文件的位置。我以为我做到了。但当将配置更改为

代码语言:javascript
复制
config = pdfkit.configuration(wkhtmltopdf='E:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27508266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档