Browse Source

Merge branch 'pdf_fix'

zwxi 9 months ago
parent
commit
492f588c67
4 changed files with 63 additions and 7 deletions
  1. 9 0
      models/english_report.go
  2. 8 0
      models/report.go
  3. 9 0
      models/smart_report/smart_report.go
  4. 37 7
      services/smart_report.go

+ 9 - 0
models/english_report.go

@@ -986,3 +986,12 @@ func UpdateEnglishReportEmailHasFail(reportId int) (err error) {
 	_, err = o.Raw(sql, reportId).Exec()
 	return
 }
+
+
+// UpdatePdfUrlEnglishReportById 清空pdf相关字段
+func UpdatePdfUrlEnglishReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE english_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
+}

+ 8 - 0
models/report.go

@@ -1166,3 +1166,11 @@ func ModifyReportImgUrl(reportId int, detailImgUrl string) (err error) {
 	_, err = o.Raw(sql, detailImgUrl, reportId).Exec()
 	return
 }
+
+// UpdatePdfUrlReportById 清空pdf相关字段
+func UpdatePdfUrlReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
+}

+ 9 - 0
models/smart_report/smart_report.go

@@ -418,3 +418,12 @@ func UpdateSmartReportsStateBySecondIds(oldState, newState int, secondIds []int)
 	_, err = o.Raw(sql, oldState, newState, secondIds).Exec()
 	return
 }
+
+
+// UpdatePdfUrlSmartReportById 清空pdf相关字段
+func UpdatePdfUrlSmartReportById(reportId int) (err error) {
+	o := orm.NewOrmUsingDB("rddp")
+	sql := `UPDATE smart_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE smart_report_id = ? `
+	_, err = o.Raw(sql, reportId).Exec()
+	return
+}

+ 37 - 7
services/smart_report.go

@@ -199,7 +199,7 @@ finally:
 	return
 }
 
-func ReportToJpeg(reportUrl, filePath string) (err error) {
+func ReportToJpeg(width int, reportUrl, filePath string) (err error) {
 	pyCode := `
 import asyncio
 from pyppeteer import launch, errors
@@ -218,7 +218,7 @@ async def main():
         
         # 设置视口大小
         await page.setViewport({
-            'width': 1920,
+            'width': %d,
             'height': 1080
         })
         
@@ -227,11 +227,15 @@ async def main():
             'waitUntil': 'networkidle0',
             'timeout': 1000000  # 设置超时时间为 100 秒
         })
-        
-        # 截取全页面的屏幕截图
+        # Customizing footer for page numbers starting from page 2
+
+        # 在这里添加两秒的等待
+        await asyncio.sleep(2)
+
         await page.screenshot({
             'path': "%s",
             'fullPage': True,
+			'quality':100
         })
         
     except errors.BrowserError as e:
@@ -256,7 +260,7 @@ finally:
     loop.close()
 `
 
-	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, reportUrl, filePath)
+	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, filePath)
 	utils.FileLog.Info("jpeg pyCode: \n" + pyCode)
 	cmd := exec.Command("python3", "-c", pyCode)
 
@@ -274,7 +278,7 @@ finally:
 	return
 }
 
-func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
+func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 	var err error
 
 	defer func() {
@@ -283,6 +287,28 @@ func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
 			utils.FileLog.Info("Report2pdfAndJpeg failed: , error: \n" + err.Error())
 		}
 	}()
+
+	// 先清空字段
+	if reportType == 1 {
+		err = models.UpdatePdfUrlReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	} else if reportType == 2 {
+		err = models.UpdatePdfUrlEnglishReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	} else if reportType == 3 {
+		err = smart_report.UpdatePdfUrlSmartReportById(reportId)
+		if err != nil {
+			utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
+			return
+		}
+	}
+
 	reportCode := utils.MD5(strconv.Itoa(reportId))
 
 	pdfPath := `./static/` + reportCode + ".pdf"
@@ -350,7 +376,11 @@ func Report2pdfAndJpeg(reportUrl string, reportId,reportType int) {
 	}()
 
 	go func() {
-		err := ReportToJpeg(reportUrl, jpegPath)
+		width := 1200
+		if reportType == 3 {
+			width = 800
+		}
+		err := ReportToJpeg(width, reportUrl, jpegPath)
 		if err != nil {
 			utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error())
 		}