Quellcode durchsuchen

PDF打水印优化

xingzai vor 3 Jahren
Ursprung
Commit
12cd600233
1 geänderte Dateien mit 11 neuen und 37 gelöschten Zeilen
  1. 11 37
      controllers/article.go

+ 11 - 37
controllers/article.go

@@ -5,6 +5,8 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/medivhzhan/weapp/v2"
+	"github.com/pdfcpu/pdfcpu/pkg/api"
+	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
@@ -13,7 +15,6 @@ import (
 	"io/ioutil"
 	nhttp "net/http"
 	"os"
-	"rdluck_tools/http"
 	"regexp"
 	"strconv"
 	"strings"
@@ -798,10 +799,6 @@ func (this *ArticleController) Pdfwatermark() {
 	if mobile == "" {
 		mobile = user.Email
 	}
-	dataMap := make(map[string]interface{})
-	dataMap["Mobile"] = mobile
-	//dataMap["Name"] = user.RealName
-	dataMap["Name"] = ""
 	pdfPath := "static/"
 	pdfUrl := fileLink
 	fileName := utils.GetRandStringNoSpecialChar(28) + ".pdf"
@@ -823,54 +820,38 @@ func (this *ArticleController) Pdfwatermark() {
 	fmt.Printf("Total length: %d", written)
 	str, _ := os.Getwd()
 	fileLink = str + "/" + pdfPath + fileName
-	dataMap["FileLink"] = fileLink
-	postUrl := "http://172.19.173.227:5007/pdfwatermark/" //
-	fmt.Println("postUrl:", postUrl)
-	postData, err := json.Marshal(dataMap)
+	oldFile := pdfPath + fileName
+	newFile := pdfPath + "new" + fileName
+	onTop := true
+	wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, "sc:4 abs, d:1, op:.4, pos:c ,points:16", onTop, 1)
 	if err != nil {
 		br.Msg = "下载失败"
-		br.ErrMsg = "解析,Marshal,Err:" + err.Error()
+		br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
 		return
 	}
-	fmt.Println("publish ppt param:", string(postData))
-	result, err := http.Post(postUrl, string(postData))
+	err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
 	if err != nil {
 		br.Msg = "下载失败"
-		br.ErrMsg = "Marshal,Err:" + err.Error()
+		br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
 		return
 	}
-	fmt.Println("publish pdf result:", string(result))
-	var pdfResult PublishPdfResult
-	err = json.Unmarshal(result, &pdfResult)
-	if err != nil {
-		br.Msg = "下载失败"
-		br.ErrMsg = "Unmarshal,Err:" + err.Error()
-		return
-	}
-	//fmt.Println(pdfResult)
 	resp := new(models.ArticleDetailFileLink)
-	filePathName := strings.TrimLeft(pdfResult.FilePath, ".")
-	//filePathOldName := strings.TrimLeft(pdfResult.FileOldPath, ".")
-	filePath := pdfResult.Pdfpath + filePathName
-	//filePathOld := pdfResult.Pdfpath + filePathOldName
 	randStr := utils.GetRandStringNoSpecialChar(28)
 	fileName = randStr + ".pdf"
 	hzUploadDir := "static/pdf/"
 	savePath := hzUploadDir + time.Now().Format("200601/20060102/")
 	savePath += fileName
 	//上传到阿里云
-	err = services.UploadFileToAliyun(fileName, filePath, savePath)
+	err = services.UploadFileToAliyun(fileName, newFile, savePath)
 	if err != nil {
 		br.Msg = "下载失败"
 		br.ErrMsg = "文件上传失败,Err:" + err.Error()
 		return
 	}
-	utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
-	utils.FileLog.Info("end update oss ")
 	fileHost := "https://hzstatic.hzinsights.com/"
 	resourceUrl := fileHost + savePath
 	defer func() {
-		os.Remove(filePath)
+		os.Remove(newFile)
 		os.Remove(fileLink)
 	}()
 	resp.FileLink = resourceUrl
@@ -880,13 +861,6 @@ func (this *ArticleController) Pdfwatermark() {
 	br.Data = resp
 }
 
-type PublishPdfResult struct {
-	Result      string `json:"result"`
-	FilePath    string `json:"file_path"`
-	FileOldPath string `json:"file_old_path"`
-	Pdfpath     string `json:"pdfpath"`
-}
-
 // @Title 同步策略报告内容
 // @Description 同步策略报告内容接口
 // @Param   ArticleIdMd5   query   int  true       "报告ID"