我试图使用pdfkit解决以下问题。
当我将wkhtmltopdf配置添加到我的应用程序时,我的应用程序无法加载到浏览器中,甚至索引页也无法加载到浏览器中。但是当我从我的应用程序中删除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-wkhtmltopdfConfiguration:
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代码:
@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 responseHTML标记:
<!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时才能正常工作,然后它会出现错误.。

错误:

发布于 2021-03-01 22:14:55
如果您在远程服务器上运行这个烧瓶应用程序,您可能很难设置wkhtmltopdf。我无法做到这一点,不得不使用python库reportlab来创建这个解决方案。
这里有一个git /代码,来自写了一本关于reportlab的书的人,如果你有兴趣把它作为解决方案的话。
https://stackoverflow.com/questions/66418180
复制相似问题