main.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # coding=utf-8
  2. import os
  3. from flask import Flask
  4. from flask import request
  5. from selenium import webdriver
  6. from selenium.webdriver.chrome.service import Service
  7. from time import sleep
  8. import html2img_ping
  9. import img_pdf_png
  10. # 配置
  11. server_port = 7109
  12. # chrome_drive_path = "E:/chromedriver-win64/chromedriver.exe" # 谷歌浏览器驱动目录
  13. chrome_drive_path = "/usr/local/bin/chromedriver" # 谷歌浏览器驱动目录
  14. image_folder = './imgs' # 转换过程中生成的图片目录, 最终会合并为一个长图
  15. output_file = './screenshot' # 最后的输出目录
  16. # hug接口输出为json格式
  17. app = Flask(__name__)
  18. @app.route('/api/report/server')
  19. def report_server():
  20. return 1
  21. @app.route('/api/report/html2img', methods=['POST'])
  22. def report_detail2img():
  23. req_data = request.get_json()
  24. if req_data is None:
  25. return {"code": 403, "data": "", "error": "参数有误"}
  26. report_url = req_data['report_url']
  27. file_name = req_data['file_name']
  28. output_type = req_data['output_type']
  29. # report_url参数, 报告的分享地址
  30. if report_url is None or report_url == "":
  31. return {"code": 403, "data": "", "error": "report_url parameter is missing"}
  32. print("report_url: ", report_url)
  33. # file_name参数, 生成的文件名
  34. if file_name is None or file_name == "":
  35. return {"code": 403, "data": "", "error": "file_name parameter is missing"}
  36. print("file_name: ", file_name)
  37. # output_type参数, 非必填, img或pdf, 为空则都生成
  38. try:
  39. # 生成图片
  40. create_img_and_pdf(report_url, file_name, output_type)
  41. # 清空imgs临时图片文件夹
  42. for fn in os.listdir(image_folder):
  43. fp = os.path.join(image_folder, fn)
  44. if os.path.isfile(fp):
  45. os.remove(fp)
  46. # 获取根目录
  47. current_file_path = os.path.abspath(__file__)
  48. project_root = os.path.dirname(current_file_path)
  49. # 生成的文件目录
  50. img_path = project_root + "/screenshot/" + file_name + ".png"
  51. pdf_path = project_root + "/screenshot/" + file_name + ".pdf"
  52. print(img_path)
  53. print(pdf_path)
  54. return {"code": 200, "data": [img_path, pdf_path], "error": ""}
  55. except Exception as e:
  56. err_msg = str(e)
  57. print(err_msg)
  58. return {"code": 403, "data": "", "error": err_msg}
  59. @app.route('/api/report/clear_local_file', methods=['POST'])
  60. def clear_local_file():
  61. req_data = request.get_json()
  62. if req_data is None:
  63. return {"code": 403, "data": "", "error": "参数有误"}
  64. file_name = req_data['file_name']
  65. if file_name is None or file_name == "":
  66. return {"code": 403, "data": "", "error": "file_name parameter is missing"}
  67. file_path = output_file + "/" + file_name
  68. if os.path.exists(file_path) is True:
  69. os.remove(file_path)
  70. print("clear success: ", file_path)
  71. return {"code": 200, "data": "", "error": ""}
  72. def create_img_and_pdf(report_url, file_name, output_type=""):
  73. # 加载驱动
  74. options = webdriver.ChromeOptions() # 创建一个谷歌WebDriver实例
  75. options.add_argument("no-sandbox")
  76. options.add_argument("disable-dev-shm-usage")
  77. options.add_argument("disable-extensions")
  78. options.add_argument("headless") # 无头浏览器模式
  79. s = Service(executable_path=chrome_drive_path)
  80. driver = webdriver.Chrome(options=options)
  81. # 加载页面
  82. print("加载页面")
  83. driver.get(report_url)
  84. sleep(5)
  85. print("加载完成")
  86. # html页面转换为图片
  87. html2img_ping.html2img(driver, image_folder)
  88. # 合并图片为长图/PDF
  89. img_pdf_png.merge_images(image_folder, output_file, file_name, output_type)
  90. driver.quit()
  91. # 退出驱动
  92. driver.quit()
  93. return
  94. if __name__ == "__main__":
  95. app.run(host='0.0.0.0', port=server_port, debug=True)