-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportExportUtil.py
63 lines (53 loc) · 1.96 KB
/
ReportExportUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import platform
import time
import pdfkit
class PDFExportUtil(object):
def start_export(self):
"""导出报告"""
# 字典存储pdf映射数据
result = dict()
file_path = f"./template/report.js"
insert_data = f"""
var reportJsonData = {json.dumps(result, indent=4, ensure_ascii=False)}
""".strip()
with open(file_path, "w", encoding="utf-8") as f:
f.write(insert_data)
self.html_to_pdf()
def html_to_pdf(self):
"""html 转 pdf"""
# 启动参数
wkhtmltopdf_options = {
'enable-local-file-access': '--enable-local-file-access',
'encoding': "utf-8",
'javascript-delay': '10000',
'header-left': '报告-[date] [time]',
'header-font-size': 8,
'header-spacing': 5,
'footer-center': '第[page] / [topage]页',
'footer-font-size': 8,
'margin-top': 10,
'dpi': 300
}
# 下载的wkhtmltopdf绝对路径,修改为自己存放的位置
if platform.system() == 'Linux':
path_wkthmltopdf = r"/usr/local/wkhtmltox/bin/wkhtmltopdf"
else:
path_wkthmltopdf = r'E:\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# html 输入 out.pdf 输出
cur_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
out_path = f"./file/report/报告({cur_time}).pdf"
pdfkit.from_file("./template/index.html", out_path, configuration=config,
options=wkhtmltopdf_options)
return True
"""更新报告路径"""
params = {
"id": r_id,
"report_path": r_path
}
sql = """
update t_report_export set report_path = %(report_path)s where id = %(id)s
"""
self.pg_conn.execute(sql, params)
self.pg_conn.commit()