基本上,我在Windows上的Flask应用程序中使用 pdfkit 将“ PDF下载页面”功能添加到该应用程序。用户可以在其中单击按钮,然后HTML页面将变成PDF文件。
pdf文件没有开始下载也没有显示,这是我收到的错误消息,后面是我的代码。 “请注意,该html文件使用CSS,JS,第三方JS库”。
OSError:wkhtmltopdf以非零代码3221225477退出。错误: 载入页面(1/6) [>。 ] 0% ... [======] 100%
app.py
@app.route("/dashboard")
def dashboard():
return render_template("dashboard.html")
@app.route("/download_pdf")
def download_pdf():
path_wkhtmltopdf = r"path/to/wkhtmltopdf.exe"
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
url = url_for("dashboard")
pdf_file = pdfkit.from_url(url, False, configuration=config)
response = make_response(pdf_file)
response.header["Content-Type"] = "application/pdf"
response.header["Content-Disposition"] = "inline; filename=output.pdf"
return response
dashboard.html
<html>
<head>
<link href="path/to/bootstrap.min.css">
<link href="style.css">
</head>
<body>
<!— html content —>
<a href='{{url_for("download_pdf")}}'>download page as pdf</a>
<script src="path/to/jquery-3.3.1.min.js"></script>
<script src="path/to/bootstrap.min.js"></script>
<script src="path/to/Chart.bundle.min.js"></script>
<script>
$(function(){
// ajax and Cahrt.js code
});
</script>
</body>
</html>