|
@@ -12,7 +12,6 @@ import (
|
|
|
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
|
|
|
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
|
|
|
"io"
|
|
|
- "math"
|
|
|
"mime/multipart"
|
|
|
"os"
|
|
|
"os/exec"
|
|
@@ -191,6 +190,7 @@ func GetResourceUrlBySvgImg(imgData string) (resourceUrl string, err error, errM
|
|
|
func GeneralWaterMarkPdf(pdfPath, waterMarkStr string) (f2 *bytes.Buffer, err error) {
|
|
|
onTop := true
|
|
|
update := false
|
|
|
+ desc := "fo:SimHei, points:24, col: 0.75 0.75 0.75, scale:1 abs, opacity:0.3"
|
|
|
//fontDir := `C:\Users\123\go\src\eta\eta_api\static`
|
|
|
fontDir := `static`
|
|
|
font.UserFontDir = fontDir
|
|
@@ -211,54 +211,73 @@ func GeneralWaterMarkPdf(pdfPath, waterMarkStr string) (f2 *bytes.Buffer, err er
|
|
|
defer func() {
|
|
|
_ = f1.Close()
|
|
|
}()
|
|
|
-
|
|
|
+ fmt.Println(len(waterMarkStr))
|
|
|
f2 = &bytes.Buffer{}
|
|
|
- points := "24" //字号大小
|
|
|
- rows := 6 //行数
|
|
|
- // 处理文字
|
|
|
- textLen := len(waterMarkStr)
|
|
|
- count := int(math.Round(250.0 / (float64(textLen) + 18)))
|
|
|
- if textLen > 102 {
|
|
|
- count = 1
|
|
|
+ wm, tmpErr := api.TextWatermark(waterMarkStr, desc, onTop, update, types.POINTS)
|
|
|
+ if tmpErr != nil {
|
|
|
+ fmt.Println("TextWatermark err:", tmpErr)
|
|
|
+ return
|
|
|
}
|
|
|
- if textLen > 210 {
|
|
|
- points = "20"
|
|
|
- rows = 7
|
|
|
+ wm.Pos = types.TopCenter
|
|
|
+ realFontSize := wm.ScaledFontSize
|
|
|
+ if realFontSize <= 0 {
|
|
|
+ realFontSize = wm.FontSize
|
|
|
}
|
|
|
- var sb1 strings.Builder
|
|
|
- for i := 0; i < count; i++ {
|
|
|
- sb1.WriteString(waterMarkStr)
|
|
|
+ targetLineWidth := font.TextWidth(waterMarkStr, wm.FontName, realFontSize)
|
|
|
+ targetText := waterMarkStr
|
|
|
+ var ctxCpu *model.Context
|
|
|
+ ctxCpu, err = api.ReadValidateAndOptimize(f1, api.LoadConfiguration())
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ var dms []types.Dim
|
|
|
+ dms, err = ctxCpu.PageDims()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- //单行文字制作完成
|
|
|
- sb1Str := sb1.String()
|
|
|
- //拼接成多行文字
|
|
|
+ dm := dms[0]
|
|
|
+ fmt.Println("targetLineWidth", targetLineWidth)
|
|
|
+ if targetLineWidth*2 > dm.Width {
|
|
|
+ targetText += " "
|
|
|
+ for _, v := range waterMarkStr {
|
|
|
+ if dm.Width <= targetLineWidth {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ targetText += fmt.Sprintf("%c", v)
|
|
|
+ targetLineWidth = font.TextWidth(targetText, wm.FontName, realFontSize)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for dm.Width >= targetLineWidth {
|
|
|
+ targetText += " " + waterMarkStr
|
|
|
+ targetLineWidth = font.TextWidth(targetText, wm.FontName, realFontSize)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("dm.Width", dm.Width, " dm.Height", dm.Height)
|
|
|
var sb2 strings.Builder
|
|
|
lineSpace := "\n \n \n \n \n \n"
|
|
|
-
|
|
|
+ rows := 6
|
|
|
for i := 0; i < rows; i++ {
|
|
|
- sb2.WriteString(sb1Str)
|
|
|
+ sb2.WriteString(targetText)
|
|
|
if i < rows-1 {
|
|
|
sb2.WriteString(lineSpace)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+ fmt.Println(sb2.String())
|
|
|
wmList := make([]*model.Watermark, 0)
|
|
|
- //for i := types.TopLeft; i <= 9; i++ {
|
|
|
- wm, tmpErr := api.TextWatermark(sb2.String(), "fo:SimHei, points:"+points+", col: 0.75 0.75 0.75, scale:1 abs, opacity:0.3", onTop, update, types.POINTS)
|
|
|
+ wm, tmpErr = api.TextWatermark(sb2.String(), "fo:SimHei, points:38, col: 0.75 0.75 0.75, scale:1 abs, opacity:0.3", onTop, update, types.POINTS)
|
|
|
if tmpErr != nil {
|
|
|
fmt.Println("TextWatermark err:", tmpErr)
|
|
|
return
|
|
|
}
|
|
|
- //wm.Pos = types.Center
|
|
|
wm.Pos = types.TopCenter
|
|
|
wmList = append(wmList, wm)
|
|
|
- //}
|
|
|
|
|
|
err = AddWatermarks(f1, f2, nil, wmList, nil)
|
|
|
-
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
func AddWatermarks(rs io.ReadSeeker, w io.Writer, selectedPages []string, wmList []*model.Watermark, conf *model.Configuration) error {
|
|
|
if rs == nil {
|
|
|
return errors.New("pdfcpu: AddWatermarks: missing rs")
|