file.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package services
  2. import (
  3. "bytes"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/pdfcpu/pdfcpu/pkg/api"
  9. "github.com/pdfcpu/pdfcpu/pkg/font"
  10. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
  11. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
  12. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
  13. "io"
  14. "mime/multipart"
  15. "os"
  16. "os/exec"
  17. "strings"
  18. "time"
  19. )
  20. // UploadToOssAndFileName 上传到oss并使用这个名称
  21. func UploadToOssAndFileName(fileMulti multipart.File, newFileName string) (resourceUrl string, err error) {
  22. dateDir := time.Now().Format("20060102")
  23. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  24. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  25. if err != nil {
  26. err = errors.New("存储目录创建失败,Err:" + err.Error())
  27. return
  28. }
  29. //本地地址
  30. fpath := uploadDir + "/" + newFileName
  31. err = saveToFile(fileMulti, fpath)
  32. if err != nil {
  33. err = errors.New("文件上传失败,Err:" + err.Error())
  34. return
  35. }
  36. //上传到阿里云 和 minio
  37. //if utils.ObjectStorageClient == "minio" {
  38. // resourceUrl, err = UploadImgToMinIo(newFileName, fpath)
  39. // if err != nil {
  40. // err = errors.New("文件上传失败,Err:" + err.Error())
  41. // return
  42. // }
  43. //} else {
  44. // resourceUrl, err = UploadAliyunV2(newFileName, fpath)
  45. // if err != nil {
  46. // err = errors.New("文件上传失败,Err:" + err.Error())
  47. // return
  48. // }
  49. //}
  50. ossClient := NewOssClient()
  51. if ossClient == nil {
  52. err = fmt.Errorf("初始化OSS服务失败")
  53. return
  54. }
  55. resourceUrl, err = ossClient.UploadFile(newFileName, fpath, "")
  56. if err != nil {
  57. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  58. return
  59. }
  60. defer func() {
  61. os.Remove(fpath)
  62. }()
  63. item := new(models.Resource)
  64. item.ResourceUrl = resourceUrl
  65. item.ResourceType = 1
  66. item.CreateTime = time.Now()
  67. _, err = models.AddResource(item)
  68. if err != nil {
  69. err = errors.New("资源上传失败,Err:" + err.Error())
  70. return
  71. }
  72. return
  73. }
  74. // saveToFile 保存到本地文件
  75. func saveToFile(fileMulti multipart.File, tofile string) error {
  76. f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  77. if err != nil {
  78. return err
  79. }
  80. defer f.Close()
  81. io.Copy(f, fileMulti)
  82. return nil
  83. }
  84. // GetResourceUrlBySvgImg
  85. // @Description: 通过svg图片生成图片资源地址(传到OSS后的地址)
  86. // @author: Roc
  87. // @datetime 2024-07-16 10:18:09
  88. // @param imgData string
  89. // @return resourceUrl string
  90. // @return err error
  91. // @return errMsg string
  92. func GetResourceUrlBySvgImg(imgData string) (resourceUrl string, err error, errMsg string) {
  93. errMsg = "图表保存失败"
  94. uploadDir := "static/images/"
  95. if !utils.FileIsExist(uploadDir) {
  96. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  97. if err != nil {
  98. err = errors.New("存储目录创建失败,Err:" + err.Error())
  99. return
  100. }
  101. }
  102. //var saveToOssPath string
  103. randStr := utils.GetRandStringNoSpecialChar(28)
  104. var fileName, outFileName string
  105. fileName = randStr + ".txt"
  106. fileName = uploadDir + fileName
  107. err = utils.SaveToFile(imgData, fileName)
  108. if err != nil {
  109. err = errors.New("图片保存失败,Err:" + err.Error())
  110. return
  111. }
  112. // 删除临时存储的svg文件
  113. defer func() {
  114. err = os.Remove(fileName)
  115. if err != nil {
  116. utils.FileLog.Info("删除临时存储的svg文件失败, err: " + err.Error())
  117. }
  118. }()
  119. outFileName = randStr + ".png"
  120. doneChannel := make(chan bool, 1)
  121. errorChannel := make(chan error, 1)
  122. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  123. go func() {
  124. output, err := cmd.CombinedOutput()
  125. if err != nil {
  126. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  127. errorChannel <- err
  128. return
  129. }
  130. doneChannel <- true
  131. }()
  132. defer func() {
  133. _ = os.Remove(outFileName)
  134. if err != nil {
  135. utils.FileLog.Info("删除生产的图片文件失败, err: " + err.Error())
  136. }
  137. }()
  138. select {
  139. case <-time.After(30 * time.Second):
  140. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  141. e := cmd.Process.Kill()
  142. if e != nil {
  143. fmt.Println("cmd kill err: ", e.Error())
  144. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  145. errMsg = "图片生成失败"
  146. err = errors.New("图片生成失败, 执行超时" + e.Error())
  147. return
  148. }
  149. fmt.Println("timeout kill process")
  150. case <-doneChannel:
  151. fmt.Println("done")
  152. case e := <-errorChannel:
  153. errMsg = "文件上传失败"
  154. err = errors.New(fmt.Sprintf("execute command failure err: %s", e.Error()))
  155. fmt.Println("execute command failure err:" + e.Error())
  156. return
  157. }
  158. //上传到阿里云 和 minio
  159. ossClient := NewOssClient()
  160. if ossClient == nil {
  161. errMsg = "上传失败"
  162. err = errors.New("初始化OSS服务失败")
  163. return
  164. }
  165. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  166. if err != nil {
  167. errMsg = "文件上传失败"
  168. err = errors.New("文件上传失败,Err:" + err.Error())
  169. return
  170. }
  171. return
  172. }
  173. func GeneralWaterMarkPdf(pdfPath, waterMarkStr string) (f2 *bytes.Buffer, err error) {
  174. onTop := true
  175. update := false
  176. desc := "fo:SimHei, points:38, rot:30, col: 0.75 0.75 0.75, scale:1 abs, opacity:0.3"
  177. fontDir := `static`
  178. font.UserFontDir = fontDir
  179. ttfList := fontDir + `/SimHei.ttf`
  180. err = api.InstallFonts([]string{ttfList})
  181. if err != nil {
  182. fmt.Println("InstallFonts err:", err)
  183. }
  184. api.DisableConfigDir()
  185. f1, err := os.Open(pdfPath)
  186. if err != nil {
  187. fmt.Println("文件不存在")
  188. return
  189. }
  190. defer func() {
  191. _ = f1.Close()
  192. }()
  193. fmt.Println(len(waterMarkStr))
  194. f2 = &bytes.Buffer{}
  195. wm, tmpErr := api.TextWatermark(waterMarkStr, desc, onTop, update, types.POINTS)
  196. if tmpErr != nil {
  197. fmt.Println("TextWatermark err:", tmpErr)
  198. return
  199. }
  200. wm.Pos = types.TopCenter
  201. realFontSize := wm.ScaledFontSize
  202. if realFontSize <= 0 {
  203. realFontSize = wm.FontSize
  204. }
  205. targetLineWidth := font.TextWidth(waterMarkStr, wm.FontName, realFontSize)
  206. targetText := waterMarkStr
  207. var ctxCpu *model.Context
  208. ctxCpu, err = api.ReadValidateAndOptimize(f1, api.LoadConfiguration())
  209. if err != nil {
  210. return nil, err
  211. }
  212. var dms []types.Dim
  213. dms, err = ctxCpu.PageDims()
  214. if err != nil {
  215. return nil, err
  216. }
  217. dm := dms[0]
  218. fmt.Println("targetLineWidth", targetLineWidth)
  219. if targetLineWidth*2 > dm.Width {
  220. targetText += " "
  221. for _, v := range waterMarkStr {
  222. if dm.Width <= targetLineWidth {
  223. break
  224. }
  225. targetText += fmt.Sprintf("%c", v)
  226. targetLineWidth = font.TextWidth(targetText, wm.FontName, realFontSize)
  227. }
  228. } else {
  229. for dm.Width >= targetLineWidth {
  230. targetText += " " + waterMarkStr
  231. targetLineWidth = font.TextWidth(targetText, wm.FontName, realFontSize)
  232. }
  233. }
  234. fmt.Println("dm.Width", dm.Width, " dm.Height", dm.Height)
  235. var sb2 strings.Builder
  236. lineSpace := "\n \n \n"
  237. rows := 8
  238. targetText1 := ""
  239. targetLength := len(targetText)
  240. for _, v := range targetText {
  241. if targetLength-len(targetText1) < 8 {
  242. break
  243. }
  244. targetText1 += fmt.Sprintf("%c", v)
  245. }
  246. //fmt.Println(targetText1)
  247. for i := 0; i < rows; i++ {
  248. if i%2 == 0 {
  249. sb2.WriteString(targetText)
  250. } else {
  251. sb2.WriteString(strings.Repeat(" ", 8))
  252. sb2.WriteString(targetText1)
  253. }
  254. if i < rows-1 {
  255. sb2.WriteString(lineSpace)
  256. }
  257. }
  258. //fmt.Println(sb2.String())
  259. wmList := make([]*model.Watermark, 0)
  260. wm, tmpErr = api.TextWatermark(sb2.String(), desc, onTop, update, types.POINTS)
  261. if tmpErr != nil {
  262. fmt.Println("TextWatermark err:", tmpErr)
  263. return
  264. }
  265. wm.Pos = types.TopCenter
  266. wmList = append(wmList, wm)
  267. err = AddWatermarks(f1, f2, nil, wmList, nil)
  268. return
  269. }
  270. func AddWatermarks(rs io.ReadSeeker, w io.Writer, selectedPages []string, wmList []*model.Watermark, conf *model.Configuration) error {
  271. if rs == nil {
  272. return errors.New("pdfcpu: AddWatermarks: missing rs")
  273. }
  274. if conf == nil {
  275. conf = model.NewDefaultConfiguration()
  276. }
  277. conf.Cmd = model.ADDWATERMARKS
  278. conf.OptimizeDuplicateContentStreams = false
  279. if len(wmList) <= 0 {
  280. return errors.New("pdfcpu: missing watermark configuration")
  281. }
  282. ctx, err := api.ReadValidateAndOptimize(rs, conf)
  283. if err != nil {
  284. return err
  285. }
  286. var pages types.IntSet
  287. pages, err = api.PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
  288. if err != nil {
  289. return err
  290. }
  291. for _, wm := range wmList {
  292. if err = pdfcpu.AddWatermarks(ctx, pages, wm); err != nil {
  293. return err
  294. }
  295. }
  296. return api.Write(ctx, w, conf)
  297. }