package services import ( "eta_gn/eta_api/models" "eta_gn/eta_api/services/alarm_msg" "eta_gn/eta_api/utils" "fmt" "os" "os/exec" "path" "strconv" "time" ) func ReportToPdf(width int, reportUrl, filePath string) (err error) { pyCode := ` import asyncio from pyppeteer import launch @asyncio.coroutine async def main(): # 异步代码 browser = await launch({ 'executablePath': '%s', 'headless': True, 'args': ['--disable-infobars', '--no-sandbox'] }) page = await browser.newPage() await page.setViewport({ 'width': %d, 'height': 1080, }) await page.goto('%s', { 'waitUntil': 'networkidle0', 'timeout': 3000000 # 设置超时时间为 100 秒 }) # 在生成PDF之前等待2秒 await asyncio.sleep(10) await page.pdf({ 'path': "%s", 'printBackground': True, 'format': "A2", 'margin': { 'top': '10mm', 'bottom': '10mm', 'left': '10mm', 'right': '10mm' } }) await browser.close() # 创建事件循环 loop = asyncio.get_event_loop() # 使用事件循环运行main函数 try: loop.run_until_complete(main()) finally: # 关闭事件循环 loop.close() ` pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, filePath) utils.FileLog.Info("pdf pyCode: \n" + pyCode) cmd := exec.Command("python3", "-c", pyCode) output, e := cmd.CombinedOutput() if e != nil { err = e utils.FileLog.Info("ReportToPdf failed: , error: \n" + err.Error()) utils.FileLog.Info("Output: %s\n", string(output)) go alarm_msg.SendAlarmMsg("ReportToPdf failed:"+err.Error(), 3) go alarm_msg.SendAlarmMsg("Output :"+string(output), 3) } defer func() { cmd.Process.Kill() }() return } func ReportToJpeg(width int, reportUrl, filePath string) (err error) { pyCode := ` import asyncio from pyppeteer import launch, errors async def main(): try: # 启动浏览器 browser = await launch({ 'executablePath': '%s', 'headless': True, 'args': ['--disable-infobars', '--no-sandbox'] }) # 新建页面 page = await browser.newPage() # 设置视口大小 await page.setViewport({ 'width': %d, 'height': 1080 }) # 导航到页面 await page.goto('%s', { 'waitUntil': 'networkidle0', 'timeout': 3000000 # 设置超时时间为 100 秒 }) # Customizing footer for page numbers starting from page 2 # 在这里添加两秒的等待 await asyncio.sleep(5) await page.screenshot({ 'path': "%s", 'fullPage': True, 'quality':80 }) except errors.BrowserError as e: print('Browser closed unexpectedly:', e) except Exception as e: print('An error occurred:', e) finally: # 确保浏览器关闭 if browser is not None: await browser.close() # 获取当前事件循环 loop = asyncio.get_event_loop() # 运行事件循环直到main协程完成 try: loop.run_until_complete(main()) except Exception as e: print('Error during event loop execution:', e) finally: # 关闭事件循环 loop.close() ` pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, filePath) utils.FileLog.Info("jpeg pyCode: \n" + pyCode) cmd := exec.Command("python3", "-c", pyCode) output, e := cmd.CombinedOutput() if e != nil { err = e utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error()) utils.FileLog.Info("Output: %s\n", string(output)) go alarm_msg.SendAlarmMsg("ReportToJpeg failed:"+err.Error(), 3) go alarm_msg.SendAlarmMsg("Output :"+string(output), 3) } defer func() { cmd.Process.Kill() }() return } // Report2pdfAndJpeg // @Description: 报告转pdf和图片 // @author: Roc // @datetime 2024-07-19 14:11:38 // @param reportUrl string // @param reportId int // @param reportType int func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) { var err error defer func() { if err != nil { go alarm_msg.SendAlarmMsg("Report2pdfAndJpeg failed:"+err.Error(), 3) utils.FileLog.Info("Report2pdfAndJpeg failed: , error: \n" + err.Error()) } }() if reportUrl == `` { return } // 先清空报告字段 err = models.UpdatePdfUrlReportById(reportId) if err != nil { utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error()) return } reportCode := utils.MD5(strconv.Itoa(reportId)) pdfPath := `./static/` + reportCode + ".pdf" jpegPath := `./static/` + reportCode + ".jpg" // 生成pdf width := 1560 if reportType == 3 { width = 800 } err = ReportToPdf(width, reportUrl, pdfPath) if err != nil { utils.FileLog.Info("ReportToPdf failed: , error: \n" + err.Error()) go alarm_msg.SendAlarmMsg("ReportToPdf failed:"+err.Error(), 3) } file, err := os.Open(pdfPath) if err != nil { utils.FileLog.Info("Open failed: , error: \n" + err.Error()) go alarm_msg.SendAlarmMsg("Open failed:"+err.Error(), 3) return } ext := path.Ext(file.Name()) randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext defer file.Close() //关闭上传文件 resourceUrl := `` ossClient := NewOssClient() if ossClient == nil { utils.FileLog.Info("初始化OSS服务失败") return } resourceUrl, err = ossClient.UploadFile(fileName, pdfPath, "") if err != nil { utils.FileLog.Info("文件上传失败, Err: \n" + err.Error()) go alarm_msg.SendAlarmMsg("文件上传失败:"+err.Error(), 3) return } defer func() { _ = os.Remove(pdfPath) }() // 更新pdf url err = models.ModifyReportPdfUrl(reportId, resourceUrl) if err != nil { utils.FileLog.Info("更新研报失败, Err: \n" + err.Error()) return } time.Sleep(1 * time.Minute) // 生成图片 err = ReportToJpeg(width, reportUrl, jpegPath) if err != nil { utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error()) } file, err = os.Open(jpegPath) if err != nil { utils.FileLog.Info("open file failed: , error: \n" + err.Error()) return } ext = path.Ext(file.Name()) randStr = utils.GetRandStringNoSpecialChar(28) fileName = randStr + ext defer file.Close() //关闭上传文件 resourceUrl = `` ossClient = NewOssClient() if ossClient == nil { utils.FileLog.Info("初始化OSS服务失败") return } resourceUrl, err = ossClient.UploadFile(fileName, jpegPath, "") if err != nil { utils.FileLog.Info("文件上传失败, Err: \n" + err.Error()) return } defer func() { _ = os.Remove(jpegPath) }() err = models.ModifyReportImgUrl(reportId, resourceUrl) if err != nil { utils.FileLog.Info("更新研报失败, Err: \n" + err.Error()) return } return }