Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eddbbb9

Browse files
authored
Add files via upload
1 parent 7c078de commit eddbbb9

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

python-flask-mysql-pdf-report/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)

python-flask-mysql-pdf-report/db.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from app import app
2+
from flaskext.mysql import MySQL
3+
4+
mysql = MySQL()
5+
6+
# MySQL configurations
7+
app.config['MYSQL_DATABASE_USER'] = 'root'
8+
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
9+
app.config['MYSQL_DATABASE_DB'] = 'roytuts'
10+
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
11+
mysql.init_app(app)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- --------------------------------------------------------
2+
-- Host: 127.0.0.1
3+
-- Server version: 8.0.17 - MySQL Community Server - GPL
4+
-- Server OS: Win64
5+
-- HeidiSQL Version: 10.2.0.5599
6+
-- --------------------------------------------------------
7+
8+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
9+
/*!40101 SET NAMES utf8 */;
10+
/*!50503 SET NAMES utf8mb4 */;
11+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
12+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
13+
14+
15+
-- Dumping database structure for roytuts
16+
DROP DATABASE IF EXISTS `roytuts`;
17+
CREATE DATABASE IF NOT EXISTS `roytuts` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
18+
USE `roytuts`;
19+
20+
-- Dumping structure for table roytuts.employee
21+
DROP TABLE IF EXISTS `employee`;
22+
CREATE TABLE IF NOT EXISTS `employee` (
23+
`emp_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
24+
`emp_first_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
25+
`emp_last_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
26+
`emp_mgr_id` int(11) DEFAULT NULL,
27+
`emp_designation` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
28+
PRIMARY KEY (`emp_id`)
29+
) ENGINE=InnoDB AUTO_INCREMENT=7975 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
30+
31+
-- Dumping data for table roytuts.employee: ~16 rows (approximately)
32+
/*!40000 ALTER TABLE `employee` DISABLE KEYS */;
33+
INSERT INTO `employee` (`emp_id`, `emp_first_name`, `emp_last_name`, `emp_mgr_id`, `emp_designation`) VALUES
34+
(7369, 'SMITH', 'JHON', 7902, 'CLERK'),
35+
(7499, 'ALLEN', 'BORDER', 7698, 'SALESMAN'),
36+
(7521, 'WARD', 'SPACE', 7698, 'SALESMAN'),
37+
(7654, 'MARTIN', 'FOWLER', 7698, 'SALESMAN'),
38+
(7698, 'BLAKE', 'RAY', NULL, 'MANAGER'),
39+
(7782, 'CLARK', 'MICHAEL', NULL, 'MANAGER'),
40+
(7788, 'SCOTT', 'TIGER', 7566, 'ANALYST'),
41+
(7839, 'KING', 'ROY', NULL, 'VICE PRESIDENT'),
42+
(7844, 'TURNER', 'RICK', 7698, 'SALESMAN'),
43+
(7876, 'ADAMS', 'EVE', 7788, 'CLERK'),
44+
(7900, 'JAMES', 'BOND', 7698, 'CLERK'),
45+
(7902, 'FORD', 'LAMBDA', 7566, 'ANALYST'),
46+
(7934, 'MILLER', 'JOHN', 7782, 'CLERK'),
47+
(7954, 'FRANK', 'JOHN', 7782, 'MANAGER'),
48+
(7964, 'MARTIN', 'HIKMAN', NULL, 'CLERK'),
49+
(7974, 'APRIL', 'HICKMAN', 7782, 'SALESMAN');
50+
/*!40000 ALTER TABLE `employee` ENABLE KEYS */;
51+
52+
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
53+
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
54+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
Binary file not shown.

python-flask-mysql-pdf-report/main.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pymysql
2+
from app import app
3+
from db import mysql
4+
from flask import Flask, Response, render_template
5+
6+
@app.route('/')
7+
def upload_form():
8+
return render_template('download.html')
9+
10+
@app.route('/download/report/pdf')
11+
def download_report():
12+
conn = None
13+
cursor = None
14+
try:
15+
conn = mysql.connect()
16+
cursor = conn.cursor(pymysql.cursors.DictCursor)
17+
18+
cursor.execute("SELECT emp_id, emp_first_name, emp_last_name, emp_designation FROM employee")
19+
result = cursor.fetchall()
20+
21+
pdf = FPDF()
22+
pdf.add_page()
23+
24+
page_width = pdf.w - 2 * pdf.l_margin
25+
26+
pdf.set_font('Times','B',14.0)
27+
pdf.cell(page_width, 0.0, 'Employee Data', align='C')
28+
pdf.ln(10)
29+
30+
pdf.set_font('Courier', '', 12)
31+
32+
col_width = page_width/4
33+
34+
pdf.ln(1)
35+
36+
th = pdf.font_size
37+
38+
for row in result:
39+
pdf.cell(col_width, th, str(row['emp_id']), border=1)
40+
pdf.cell(col_width, th, row['emp_first_name'], border=1)
41+
pdf.cell(col_width, th, row['emp_last_name'], border=1)
42+
pdf.cell(col_width, th, row['emp_designation'], border=1)
43+
pdf.ln(th)
44+
45+
pdf.ln(10)
46+
47+
pdf.set_font('Times','',10.0)
48+
pdf.cell(page_width, 0.0, '- end of report -', align='C')
49+
50+
return Response(pdf.output(dest='S').encode('latin-1'), mimetype='application/pdf', headers={'Content-Disposition':'attachment;filename=employee_report.pdf'})
51+
except Exception as e:
52+
print(e)
53+
finally:
54+
cursor.close()
55+
conn.close()
56+
57+
if __name__ == "__main__":
58+
app.run()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/generate-pdf-report-from-mysql-database-using-python-flask/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!doctype html>
2+
<title>Python Flask File Generate PDF Report from MySQL</title>
3+
<h2>Generate PDF Report from MySQL</h2>
4+
5+
<p>
6+
<a href="{{ url_for('.download_report') }}">Generate Pdf Report</a>
7+
</p>

0 commit comments

Comments
 (0)