|
@@ -0,0 +1,106 @@
|
|
|
+import hug
|
|
|
+import os
|
|
|
+from bottle import run
|
|
|
+from selenium import webdriver
|
|
|
+from selenium.webdriver.chrome.service import Service
|
|
|
+from time import sleep
|
|
|
+
|
|
|
+import html2img_ping
|
|
|
+import img_pdf_png
|
|
|
+
|
|
|
+# 配置
|
|
|
+server_port = 7109
|
|
|
+# chrome_drive_path = "E:/chromedriver-win64/chromedriver.exe" # 谷歌浏览器驱动目录
|
|
|
+chrome_drive_path = "/usr/local/bin/chromedriver" # 谷歌浏览器驱动目录
|
|
|
+image_folder = './imgs' # 转换过程中生成的图片目录, 最终会合并为一个长图
|
|
|
+output_file = './screenshot' # 最后的输出目录
|
|
|
+
|
|
|
+# hug接口输出为json格式
|
|
|
+hug.API(__name__).http.output_format = hug.output_format.json
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/api/report/server')
|
|
|
+def report_server():
|
|
|
+ return 1
|
|
|
+
|
|
|
+
|
|
|
+@hug.post('/api/report/html2img')
|
|
|
+def report_detail2img(report_url: hug.types.text, file_name: hug.types.text, output_type: hug.types.text):
|
|
|
+ # report_url参数, 报告的分享地址
|
|
|
+ if report_url is None or report_url == "":
|
|
|
+ return {"code": 403, "data": "", "error": "report_url parameter is missing"}
|
|
|
+ print("report_url: ", report_url)
|
|
|
+
|
|
|
+ # file_name参数, 生成的文件名
|
|
|
+ if file_name is None or file_name == "":
|
|
|
+ return {"code": 403, "data": "", "error": "file_name parameter is missing"}
|
|
|
+ print("file_name: ", file_name)
|
|
|
+
|
|
|
+ # output_type参数, 非必填, img或pdf, 为空则都生成
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 生成图片
|
|
|
+ create_img_and_pdf(report_url, file_name, output_type)
|
|
|
+
|
|
|
+ # 清空imgs临时图片文件夹
|
|
|
+ for fn in os.listdir(image_folder):
|
|
|
+ fp = os.path.join(image_folder, fn)
|
|
|
+ if os.path.isfile(fp):
|
|
|
+ os.remove(fp)
|
|
|
+
|
|
|
+ # 获取根目录
|
|
|
+ current_file_path = os.path.abspath(__file__)
|
|
|
+ project_root = os.path.dirname(current_file_path)
|
|
|
+
|
|
|
+ # 生成的文件目录
|
|
|
+ img_path = project_root + "/screenshot/" + file_name + ".png"
|
|
|
+ pdf_path = project_root + "/screenshot/" + file_name + ".pdf"
|
|
|
+ print(img_path)
|
|
|
+ print(pdf_path)
|
|
|
+ return {"code": 200, "data": [img_path, pdf_path], "error": ""}
|
|
|
+ except Exception as e:
|
|
|
+ err_msg = str(e)
|
|
|
+ print(err_msg)
|
|
|
+ return {"code": 403, "data": "", "error": err_msg}
|
|
|
+
|
|
|
+
|
|
|
+@hug.post('/api/report/clear_local_file')
|
|
|
+def clear_local_file(file_name: hug.types.text):
|
|
|
+ if file_name is None or file_name == "":
|
|
|
+ return {"code": 403, "data": "", "error": "file_name parameter is missing"}
|
|
|
+
|
|
|
+ file_path = output_file + "/" + file_name
|
|
|
+ if os.path.exists(file_path) is True:
|
|
|
+ os.remove(file_path)
|
|
|
+ print("clear success: ", file_path)
|
|
|
+ return {"code": 200, "data": "", "error": ""}
|
|
|
+
|
|
|
+
|
|
|
+def create_img_and_pdf(report_url, file_name, output_type=""):
|
|
|
+ # 加载驱动
|
|
|
+ options = webdriver.ChromeOptions() # 创建一个谷歌WebDriver实例
|
|
|
+ options.add_argument("headless") # 无头浏览器模式
|
|
|
+ s = Service(executable_path=chrome_drive_path)
|
|
|
+ driver = webdriver.Chrome(service=s, options=options)
|
|
|
+
|
|
|
+ # 加载页面
|
|
|
+ print("加载页面")
|
|
|
+ driver.get(report_url)
|
|
|
+ sleep(5)
|
|
|
+ print("加载完成")
|
|
|
+
|
|
|
+ # html页面转换为图片
|
|
|
+ html2img_ping.html2img(driver, image_folder)
|
|
|
+
|
|
|
+ # 合并图片为长图/PDF
|
|
|
+ img_pdf_png.merge_images(image_folder, output_file, file_name, output_type)
|
|
|
+ driver.quit()
|
|
|
+
|
|
|
+ # 退出驱动
|
|
|
+ driver.quit()
|
|
|
+ return
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ app = __hug__.http.server()
|
|
|
+ run(app=app, reloader=True, port=server_port)
|