# coding=utf-8 import os from flask import Flask from flask import request 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格式 app = Flask(__name__) @app.route('/api/report/server') def report_server(): return 1 @app.route('/api/report/html2img', methods=['POST']) def report_detail2img(): req_data = request.get_json() if req_data is None: return {"code": 403, "data": "", "error": "参数有误"} report_url = req_data['report_url'] file_name = req_data['file_name'] output_type = req_data['output_type'] # 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} @app.route('/api/report/clear_local_file', methods=['POST']) def clear_local_file(): req_data = request.get_json() if req_data is None: return {"code": 403, "data": "", "error": "参数有误"} file_name = req_data['file_name'] 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("no-sandbox") options.add_argument("disable-dev-shm-usage") options.add_argument("disable-extensions") options.add_argument("headless") # 无头浏览器模式 s = Service(executable_path=chrome_drive_path) driver = webdriver.Chrome(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.run(host='0.0.0.0', port=server_port, debug=True)