main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # coding=utf-8
  2. import hug
  3. import os
  4. from bottle import run
  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. hug.API(__name__).http.output_format = hug.output_format.json
  18. @hug.get('/api/report/server')
  19. def report_server():
  20. return 1
  21. @hug.post('/api/report/html2img')
  22. def report_detail2img(report_url: hug.types.text, file_name: hug.types.text, output_type: hug.types.text):
  23. # report_url参数, 报告的分享地址
  24. if report_url is None or report_url == "":
  25. return {"code": 403, "data": "", "error": "report_url parameter is missing"}
  26. print("report_url: ", report_url)
  27. # file_name参数, 生成的文件名
  28. if file_name is None or file_name == "":
  29. return {"code": 403, "data": "", "error": "file_name parameter is missing"}
  30. print("file_name: ", file_name)
  31. # output_type参数, 非必填, img或pdf, 为空则都生成
  32. try:
  33. # 生成图片
  34. create_img_and_pdf(report_url, file_name, output_type)
  35. # 清空imgs临时图片文件夹
  36. for fn in os.listdir(image_folder):
  37. fp = os.path.join(image_folder, fn)
  38. if os.path.isfile(fp):
  39. os.remove(fp)
  40. # 获取根目录
  41. current_file_path = os.path.abspath(__file__)
  42. project_root = os.path.dirname(current_file_path)
  43. # 生成的文件目录
  44. img_path = project_root + "/screenshot/" + file_name + ".png"
  45. pdf_path = project_root + "/screenshot/" + file_name + ".pdf"
  46. print(img_path)
  47. print(pdf_path)
  48. return {"code": 200, "data": [img_path, pdf_path], "error": ""}
  49. except Exception as e:
  50. err_msg = str(e)
  51. print(err_msg)
  52. return {"code": 403, "data": "", "error": err_msg}
  53. @hug.post('/api/report/clear_local_file')
  54. def clear_local_file(file_name: hug.types.text):
  55. if file_name is None or file_name == "":
  56. return {"code": 403, "data": "", "error": "file_name parameter is missing"}
  57. file_path = output_file + "/" + file_name
  58. if os.path.exists(file_path) is True:
  59. os.remove(file_path)
  60. print("clear success: ", file_path)
  61. return {"code": 200, "data": "", "error": ""}
  62. def create_img_and_pdf(report_url, file_name, output_type=""):
  63. # 加载驱动
  64. options = webdriver.ChromeOptions() # 创建一个谷歌WebDriver实例
  65. options.add_argument("no-sandbox")
  66. options.add_argument("disable-dev-shm-usage")
  67. options.add_argument("disable-extensions")
  68. options.add_argument("headless") # 无头浏览器模式
  69. s = Service(executable_path=chrome_drive_path)
  70. driver = webdriver.Chrome(options=options)
  71. # 加载页面
  72. print("加载页面")
  73. driver.get(report_url)
  74. sleep(5)
  75. print("加载完成")
  76. # html页面转换为图片
  77. html2img_ping.html2img(driver, image_folder)
  78. # 合并图片为长图/PDF
  79. img_pdf_png.merge_images(image_folder, output_file, file_name, output_type)
  80. driver.quit()
  81. # 退出驱动
  82. driver.quit()
  83. return
  84. if __name__ == "__main__":
  85. app = __hug__.http.server()
  86. run(app=app, reloader=True, port=server_port)