浏览代码

Merge branch 'feature/eta_2.6.9' into debug

hsun 6 天之前
父节点
当前提交
b7400b70a8
共有 1 个文件被更改,包括 67 次插入14 次删除
  1. 67 14
      services/smart_report.go

+ 67 - 14
services/smart_report.go

@@ -9,10 +9,12 @@ import (
 	"eta/eta_api/utils"
 	"fmt"
 	"html"
+	"math"
 	"os"
 	"os/exec"
 	"path"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -137,7 +139,7 @@ func SmartReportElasticUpsert(smartReportId int, state int) (err error) {
 	return
 }
 
-func ReportToPdf(width int, reportUrl, filePath string, top, bottom, left, right int) (err error) {
+func ReportToPdf(width, height int, reportUrl, filePath string, top, bottom, left, right int) (err error) {
 	pyCode := `
 import asyncio
 from pyppeteer import launch
@@ -152,7 +154,7 @@ async def main():
     page = await browser.newPage()
     await page.setViewport({
         'width': %d,
-        'height': 1697
+        'height': %d
     })
     await page.goto('%s', {
         'waitUntil': 'networkidle0',
@@ -164,7 +166,7 @@ async def main():
 
     await page.pdf({
 		'width': %d,
-        'height': 1697,
+        'height': %d,
         'path': "%s",
         'printBackground': True,
         'margin': {
@@ -187,7 +189,7 @@ finally:
     loop.close()
 `
 
-	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, width+left+right, filePath, top, bottom, left, right)
+	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, height, reportUrl, width+left+right, height+top+bottom, filePath, top, bottom, left, right)
 	utils.FileLog.Info("pdf pyCode: \n" + pyCode)
 	cmd := exec.Command(utils.CommandPython, "-c", pyCode)
 	output, e := cmd.CombinedOutput()
@@ -204,7 +206,7 @@ finally:
 	return
 }
 
-func ReportToJpeg(width int, reportUrl, filePath string) (err error) {
+func ReportToJpeg(width, height int, reportUrl, filePath string) (err error) {
 	pyCode := `
 import asyncio
 from pyppeteer import launch, errors
@@ -224,7 +226,7 @@ async def main():
         # 设置视口大小
         await page.setViewport({
             'width': %d,
-            'height': 1697
+            'height': %d
         })
         
         # 导航到页面
@@ -265,7 +267,7 @@ finally:
     loop.close()
 `
 
-	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, reportUrl, filePath)
+	pyCode = fmt.Sprintf(pyCode, utils.ChromePath, width, height, reportUrl, filePath)
 	utils.FileLog.Info("jpeg pyCode: \n" + pyCode)
 	cmd := exec.Command(utils.CommandPython, "-c", pyCode)
 	output, e := cmd.CombinedOutput()
@@ -319,7 +321,6 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 			return
 		}
 	} else {
-
 		defer func() {
 			if err != nil {
 				go alarm_msg.SendAlarmMsg("Report2pdfAndJpeg failed:"+err.Error(), 3)
@@ -332,6 +333,13 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 		}
 		var report *models.ReportDetail
 
+		// 默认宽高,自由布局需要根据比例重新计算
+		var freeRatio string
+		pcWidth := 1200
+		pcHeight := 1697
+		mobileWidth := 600
+		mobileHeight := 1697
+
 		// 先清空字段
 		if reportType == 1 {
 			report, err = models.GetReportById(reportId)
@@ -343,6 +351,9 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 				utils.FileLog.Info("清空pdf长图字段失败, Err: \n" + err.Error())
 				return
 			}
+			if report.ReportLayout == 3 {
+				freeRatio = report.FreeReportRatio
+			}
 		} else if reportType == 2 {
 			err = models.UpdatePdfUrlEnglishReportById(reportId)
 			if err != nil {
@@ -357,6 +368,23 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 			}
 		}
 
+		// 7/10为A4的比例,不需要重新算
+		if freeRatio != "" && freeRatio != "7/10" {
+			h, e := calculateReportPdfHeight(pcHeight, freeRatio)
+			if e != nil {
+				err = fmt.Errorf("重新计算PDF生成高度失败, %v", e)
+				return
+			}
+			pcHeight = h
+
+			hm, e := calculateReportPdfHeight(mobileHeight, freeRatio)
+			if e != nil {
+				err = fmt.Errorf("重新计算移动端PDF生成高度失败, %v", e)
+				return
+			}
+			mobileHeight = hm
+		}
+
 		reportCode := utils.MD5(strconv.Itoa(reportId))
 
 		// pc端
@@ -364,7 +392,7 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 			pdfPath := `./static/` + reportCode + "_1200.pdf"
 			jpegPath := `./static/` + reportCode + "_1200.jpg"
 
-			width := 1200
+			//width := 1200
 			top, bottom, left, right := 20, 20, 20, 20
 			if reportType == 1 {
 				if report != nil && report.ReportLayout == 3 {
@@ -374,7 +402,7 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 			//if reportType == 3 {
 			//	width = 800
 			//}
-			err = ReportToPdf(width, reportUrl, pdfPath, top, bottom, left, right)
+			err = ReportToPdf(pcWidth, pcHeight, reportUrl, pdfPath, top, bottom, left, right)
 			if err != nil {
 				utils.FileLog.Info("ReportToPdf failed: , error: \n" + err.Error())
 				go alarm_msg.SendAlarmMsg("ReportToPdf failed:"+err.Error(), 3)
@@ -434,7 +462,7 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 
 			time.Sleep(1 * time.Minute)
 
-			err = ReportToJpeg(width, reportUrl, jpegPath)
+			err = ReportToJpeg(pcWidth, pcHeight, reportUrl, jpegPath)
 			if err != nil {
 				utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error())
 			}
@@ -500,8 +528,8 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 				pdfPathMobile := `./static/` + reportCode + "_600.pdf"
 				jpegPathMobile := `./static/` + reportCode + "_600.jpg"
 				top, bottom, left, right := 20, 20, 20, 20
-				width := 600
-				err = ReportToPdf(width, reportUrl, pdfPathMobile, top, bottom, left, right)
+				//width := 600
+				err = ReportToPdf(mobileWidth, mobileHeight, reportUrl, pdfPathMobile, top, bottom, left, right)
 				if err != nil {
 					utils.FileLog.Info("ReportToPdf failed: , error: \n" + err.Error())
 					go alarm_msg.SendAlarmMsg("ReportToPdf failed:"+err.Error(), 3)
@@ -561,7 +589,7 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 
 				time.Sleep(1 * time.Minute)
 
-				err = ReportToJpeg(width, reportUrl, jpegPathMobile)
+				err = ReportToJpeg(mobileWidth, mobileHeight, reportUrl, jpegPathMobile)
 				if err != nil {
 					utils.FileLog.Info("ReportToJpeg failed: , error: \n" + err.Error())
 				}
@@ -618,3 +646,28 @@ func Report2pdfAndJpeg(reportUrl string, reportId, reportType int) {
 		}
 	}
 }
+
+// calculateReportPdfHeight 自由布局-根据宽度和布局比例计算高度
+func calculateReportPdfHeight(width int, freeRatio string) (height int, err error) {
+	ratioArr := strings.Split(freeRatio, "/")
+	if len(ratioArr) != 2 {
+		err = fmt.Errorf("布局比例有误, %s", freeRatio)
+		return
+	}
+	m, e := strconv.ParseFloat(ratioArr[0], 64)
+	if e != nil {
+		err = fmt.Errorf("布局比例有误, %s", freeRatio)
+		return
+	}
+	n, e := strconv.ParseFloat(ratioArr[1], 64)
+	if e != nil {
+		err = fmt.Errorf("布局比例有误, %s", freeRatio)
+		return
+	}
+	if n <= 0 {
+		err = fmt.Errorf("布局比例有误, %s", freeRatio)
+		return
+	}
+	height = int(math.Round(float64(width) * m / n))
+	return
+}