我试图从Flask端点创建一个PDF文档,它基本上从MySql数据库中检索值并在HTML页面上显示它。我试过jsPDF但是无法得到结果
这是我的Python代码:
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
# configure db
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'username'
app.config['MYSQL_PASSWORD'] = 'dbpassword'
app.config['MYSQL_DB'] = 'flaskapp'
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def alpha():
if request.method == 'POST':
# Fetch the form data
userDetails = request.form
name = userDetails['name']
email = userDetails['email']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users(name,email) VALUES(%s,%s)", (name, email))
mysql.connection.autocommit(on=True)
cur.close()
return 'success'
return render_template('index.html')
@app.route('/users')
def users():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT * from users")
if resultValue > 0:
userDetails = cur.fetchall()
return render_template('users.html', userDetails=userDetails)
@app.route('/showpdf')
def showpdf():
return render_template('showpdf.html')
if __name__ == '__main__':
app.run()
以下是html页面:users.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display Records From DB</title>
</head>
<body>
<style>
th {
background-color: orange;
}
</style>
<table border="1">
<tr>
<th>
Name
</th>
<th>
Email Id
</th>
</tr>
{% for users in userDetails %}
<tr>
<td>{{users[0]}}</td>
<td>{{users[1]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
index.html的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DB Adder</title>
</head>
<body>
<form method="POST" action="">
Name <input type="text" name="name"/>
<br>
Email <input type="email" name="email"/>
<br>
<input type="submit">
</form>
</body>
</html>
最后我想要生成pdf的页面,并使用jsPDF showpdf.html通过简单的点击下载它
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PDF PAGE</title>
<script type="text/javascript" src="/jspdf.min.js"></script>
<script>
function genPDF(){
var doc = new jsPDF();
doc.text(20,20,'Here is the pdf from the endpoint : localhost:5000/showpdf');
doc.addPage();
doc.save('localhost:5000/users');
}
</script>
</head>
<body>
<h1>This page will output a pdf using jsPDF</h1>
<a href="javascript:genPDF()">Click To Download PDF</a>
</body>
</html>
我认为你误解了后端是烧瓶和前端是html和javascript。
当您单击<a>
标记时,浏览器会将您的请求发送到支持的烧瓶,但您的文件系统中没有任何pdf文档。这就是你得到404错误的原因。
也许你可以在前端生成pdf(我不熟悉jsPDf库)。至少做以下事情:
你可以使用flask_weasyprint
:
from flask_weasyprint import HTML, render_pdf
import flask
app = flask.Flask(__name__)
@app.route('/users')
def users():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT * from users")
if resultValue > 0:
userDetails = cur.fetchall()
html = render_template('users.html', userDetails=userDetails)
return render_pdf(HTML(string=html))
现在,一旦用户导航到/users
,下载就会开始。
基于Ajax1234解决方案和您遇到的问题..
您的问题是烧瓶无法找到安装flask_weasyprint的位置。
如果您在Windows上,则必须导航到/ script目录以进行安装
cd C:\flaskproject\flask\Scripts
C:\flaskproject\flask\Scripts>pip install Flask-WeasyPrint
然后当你运行你的应用程序。一切都会好起来的