zwxi 10 months ago
parent
commit
d7daacd349
1 changed files with 91 additions and 56 deletions
  1. 91 56
      services/smart_report.go

+ 91 - 56
services/smart_report.go

@@ -135,44 +135,62 @@ func SmartReportElasticUpsert(smartReportId int, state int) (err error) {
 func ReportToPdf(reportUrl, filePath string) (err error) {
 	pyCode := `
 import asyncio
-from pyppeteer import launch
+from pyppeteer import launch, errors
 
 @asyncio.coroutine
 def main():
-    # 异步代码
-    browser = yield from launch({
-        'executablePath': '%s',
-        'headless': True,
-        'args': ['--disable-infobars', '--no-sandbox']
-    })
-    page = yield from browser.newPage()
-    yield from page.setViewport({
-        'width': 1920,
-        'height': 1080,
-    })
-    yield from page.goto('%s', {
-        'waitUntil': 'networkidle0',
-        'timeout': 1000000  # 设置超时时间为 100 秒
-    })
-    yield from page.pdf({
-        'path': "%s",
-        'printBackground': True,
-        'format': "A2",
-        'margin': {
-            'top': '10mm',
-            'bottom': '10mm',
-            'left': '10mm',
-            'right': '10mm'
-        }
-    })
-    yield from browser.close()
-
-# 创建事件循环
+    try:
+        # 启动浏览器
+        browser = yield from launch({
+            'executablePath': '%s',
+            'headless': True,
+            'args': ['--disable-infobars', '--no-sandbox']
+        })
+        
+        # 新建页面
+        page = yield from browser.newPage()
+        
+        # 设置视口大小
+        yield from page.setViewport({
+            'width': 1920,
+            'height': 1080
+        })
+        
+        # 导航到页面
+        yield from page.goto('%s', {
+            'waitUntil': 'networkidle0',
+            'timeout': 1000000  # 设置超时时间为 100 秒
+        })        
+        # 生成PDF
+        yield from page.pdf({
+            'path': '%s',
+            'printBackground': True,
+            'format': 'A4',
+            'margin': {
+                'top': '10mm',
+                'bottom': '10mm',
+                'left': '10mm',
+                'right': '10mm'
+            }
+        })
+        
+    except errors.BrowserError as e:
+        print('Browser closed unexpectedly:', e)
+    except Exception as e:
+        print('An error occurred:', e)
+    finally:
+        # 确保浏览器关闭
+        if 'browser' in locals():
+            yield from browser.close()
+
+# 获取当前事件循环
 loop = asyncio.get_event_loop()
 
-# 使用事件循环运行main函数
+# 运行事件循环直到main协程完成
 try:
     loop.run_until_complete(main())
+except Exception as e:
+    print('Error during event loop execution:', e)
 finally:
     # 关闭事件循环
     loop.close()
@@ -191,38 +209,55 @@ finally:
 func ReportToJpeg(reportUrl, filePath string) (err error) {
 	pyCode := `
 import asyncio
-from pyppeteer import launch
+from pyppeteer import launch, errors
 
 @asyncio.coroutine
 def main():
-    # 异步代码
-    browser = yield from launch({
-        'executablePath': '%s',
-        'headless': True,
-        'args': ['--disable-infobars', '--no-sandbox']
-    })
-    page = yield from browser.newPage()
-    yield from page.setViewport({
-        'width': 1920,
-        'height': 1080,
-    })
-    yield from page.goto('%s', {
-        'waitUntil': 'networkidle0',
-        'timeout': 1000000  # 设置超时时间为 100 秒
-    })
-    yield from page.screenshot({
-        'path': "%s",
-        'type': "jpeg",
-        'fullPage': True,
-    })
-    yield from browser.close()
-
-# 创建事件循环
+    try:
+        # 启动浏览器
+        browser = yield from launch({
+            'executablePath': '%s',
+            'headless': True,
+            'args': ['--disable-infobars', '--no-sandbox']
+        })
+        
+        # 新建页面
+        page = yield from browser.newPage()
+        
+        # 设置视口大小
+        yield from page.setViewport({
+            'width': 1920,
+            'height': 1080
+        })
+        
+        # 导航到页面
+        yield from page.goto('%s', {
+            'waitUntil': 'networkidle0',
+            'timeout': 1000000  # 设置超时时间为 100 秒
+        })        
+		yield from page.screenshot({
+			'path': "%s",
+			'type': "jpeg",
+			'fullPage': True,
+    	})
+        
+    except errors.BrowserError as e:
+        print('Browser closed unexpectedly:', e)
+    except Exception as e:
+        print('An error occurred:', e)
+    finally:
+        # 确保浏览器关闭
+        if 'browser' in locals():
+            yield from browser.close()
+
+# 获取当前事件循环
 loop = asyncio.get_event_loop()
 
-# 使用事件循环运行main函数
+# 运行事件循环直到main协程完成
 try:
     loop.run_until_complete(main())
+except Exception as e:
+    print('Error during event loop execution:', e)
 finally:
     # 关闭事件循环
     loop.close()