首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用烧瓶将html表格转换成pdf

用烧瓶将html表格转换成pdf
EN

Stack Overflow用户
提问于 2021-03-01 07:35:34
回答 1查看 1.2K关注 0票数 1

我试图使用pdfkit解决以下问题。

当我将wkhtmltopdf配置添加到我的应用程序时,我的应用程序无法加载到浏览器中,甚至索引页也无法加载到浏览器中。但是当我从我的应用程序中删除wkhtmltopdf配置时,我的所有路由都正常工作,只有HTML到pdf路由给我以下错误。

代码语言:javascript
复制
OSError: No wkhtmltopdf executable found: "b''"
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

Configuration:

代码语言:javascript
复制
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_url('http://127.0.0.1:5000/leave_worker_pdf', 'output.pdf', configuration=config)

route.py代码:

代码语言:javascript
复制
@app.route('/leave_worker_pdf', methods=['POST'])
def leave_worker_pdf():
    leave_worker = LeaveWorker.query.all()
    html = render_template("leave_worker_pdf.html", leave_worker=leave_worker)
    pdf = pdfkit.from_string(html, False)
    response = make_response(pdf)
    response.headers["Content-Type"] = "application/pdf"
    response.headers["Content-Disposition"] = "inline; filename=output.pdf"
    return response

HTML标记:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
</head>
<body>
  <div class="container">

  
    <h1 class="text-center mt-4">Leave Workers Details</h1>
  
    <table class="table mt-4">
        <thead class="table-dark">
          <tr>
            <th scope="col">Id</th>
            <th scope="col">Name</th>
            <th scope="col">Address</th>
            <th scope="col">Contact</th>
            <th scope="col">LeaveDate</th>
            <th scope="col">Admin</th>
          </tr>
        </thead>
        {% for worker in leave_worker %}
        <tbody>
          <tr>
            <td> {{ worker.id }}</td>
            <td> {{ worker.leave_worker_name }} </td>
            <td> {{ worker.leave_worker_address }} </td>
            <td> {{ worker.leave_worker_contact }} </td>
            <td> {{ worker.leave_date.strftime('%Y-%m-%d') }} </td>
            <td> {{ worker.admin.username }} </td>
          </tr>
        </tbody>
        {% endfor %}
    </table>
  
  </div>
</body>
</html>

当我添加wkhtmltopdf配置时,页面无法加载到browser.

甚至主页都不能加载到浏览器.中。

当我从我的应用程序中删除wkhtmltopdf配置时,我的所有路由只有在我单击pdf时才能正常工作,然后它会出现错误.

错误:

EN

回答 1

Stack Overflow用户

发布于 2021-03-01 22:14:55

如果您在远程服务器上运行这个烧瓶应用程序,您可能很难设置wkhtmltopdf。我无法做到这一点,不得不使用python库reportlab来创建这个解决方案。

这里有一个git /代码,来自写了一本关于reportlab的书的人,如果你有兴趣把它作为解决方案的话。

https://github.com/driscollis/reportlabbookcode

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66418180

复制
相关文章

相似问题

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