简体   繁体   English

使用 flask 将 html 表转换为 pdf

[英]Convert html table into pdf using flask

I'm tried to solve the following problem using pdfkit.我尝试使用 pdfkit 解决以下问题。

when I add wkhtmltopdf configuration to my app then my app can't load into the browser even the index page cant load into the browser.当我将 wkhtmltopdf 配置添加到我的应用程序时,我的应用程序无法加载到浏览器中,即使索引页面也无法加载到浏览器中。 but when I remove wkhtmltopdf configuration from my app then my all routes work fine only the HTML to pdf route gives me the following error.但是当我从我的应用程序中删除 wkhtmltopdf 配置时,我的所有路由都可以正常工作,只有 HTML 到 pdf 路由给了我以下错误。

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:配置:

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 code: route.py 代码:

@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 markup: HTML 标记:

<!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>

When I add wkhtmltopdf configuration the page can't load into the browser.当我添加 wkhtmltopdf 配置时,页面无法加载到浏览器中。

当我添加 wkhtmltopdf 配置时,页面无法加载到浏览器中

Even the homepage cant load into the browser.甚至主页也无法加载到浏览器中。

索引页

when I remove wkhtmltopdf configuration from my app then my all routes work fine only when i click on the pdf then its give in error.当我从我的应用程序中删除 wkhtmltopdf 配置时,只有当我单击 pdf 时,我的所有路由才能正常工作,然后它会出错。

在此处输入图像描述

Error:错误:

在此处输入图像描述

If you're running this flask application on a remote server, you might have difficulty setting up wkhtmltopdf.如果您在远程服务器上运行此 flask 应用程序,则可能难以设置 wkhtmltopdf。 I was unable to do so and had to resort to using python library reportlab in order to create this solution.我无法这样做,不得不求助于使用 python 库 reportlab 来创建此解决方案。

Here's a git repo w/ code from someone who wrote a book on reportlab if you're interested in pursuing that as a solution.这是一个 git 存储库,带有代码,如果您有兴趣将其作为解决方案,请写关于 reportlab 的书。

https://github.com/driscollis/reportlabbookcode https://github.com/driscollis/reportlabbookcode

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

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