|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/pdfcpu/pdfcpu/pkg/api" |
| 10 | + "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" |
| 11 | + "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" |
| 12 | + "github.com/phpdave11/gofpdf" |
| 13 | +) |
| 14 | + |
| 15 | +func generateWatermarkPDF(text string) (string, func(), error) { |
| 16 | + tmpDir, err := os.MkdirTemp("", "cups-web-watermark-*") |
| 17 | + if err != nil { |
| 18 | + return "", nil, fmt.Errorf("create temp dir: %w", err) |
| 19 | + } |
| 20 | + cleanup := func() { os.RemoveAll(tmpDir) } |
| 21 | + |
| 22 | + pdf := gofpdf.New("P", "mm", "A4", "") |
| 23 | + pdf.SetMargins(0, 0, 0) |
| 24 | + pdf.SetAutoPageBreak(false, 0) |
| 25 | + pdf.AddPage() |
| 26 | + |
| 27 | + if err := setPdfTextFont(pdf, 28); err != nil { |
| 28 | + cleanup() |
| 29 | + return "", nil, fmt.Errorf("load font: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + pdf.SetTextColor(180, 180, 180) |
| 33 | + pdf.SetAlpha(0.15, "") |
| 34 | + |
| 35 | + pageW, pageH := pdf.GetPageSize() |
| 36 | + textW := pdf.GetStringWidth(text) |
| 37 | + if textW < 10 { |
| 38 | + textW = 10 |
| 39 | + } |
| 40 | + |
| 41 | + stepX := textW + 30 |
| 42 | + stepY := 45.0 |
| 43 | + diag := math.Sqrt(pageW*pageW + pageH*pageH) |
| 44 | + |
| 45 | + for y := -diag / 2; y < pageH+diag/2; y += stepY { |
| 46 | + for x := -diag / 2; x < pageW+diag/2; x += stepX { |
| 47 | + pdf.TransformBegin() |
| 48 | + pdf.TransformRotate(45, x, y) |
| 49 | + pdf.Text(x, y, text) |
| 50 | + pdf.TransformEnd() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + outPath := filepath.Join(tmpDir, "watermark.pdf") |
| 55 | + if err := pdf.OutputFileAndClose(outPath); err != nil { |
| 56 | + cleanup() |
| 57 | + return "", nil, fmt.Errorf("write watermark pdf: %w", err) |
| 58 | + } |
| 59 | + return outPath, cleanup, nil |
| 60 | +} |
| 61 | + |
| 62 | +func applyWatermarkToPDF(inputPath, watermarkText string) (string, func(), error) { |
| 63 | + wmPath, wmCleanup, err := generateWatermarkPDF(watermarkText) |
| 64 | + if err != nil { |
| 65 | + return "", nil, fmt.Errorf("generate watermark: %w", err) |
| 66 | + } |
| 67 | + defer wmCleanup() |
| 68 | + |
| 69 | + wmFile, err := os.Open(wmPath) |
| 70 | + if err != nil { |
| 71 | + return "", nil, fmt.Errorf("open watermark pdf: %w", err) |
| 72 | + } |
| 73 | + defer wmFile.Close() |
| 74 | + |
| 75 | + wm, err := api.PDFWatermarkForReadSeeker(wmFile, 1, "scalefactor:1 rel, opacity:1", true, false, types.POINTS) |
| 76 | + if err != nil { |
| 77 | + return "", nil, fmt.Errorf("create watermark config: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + tmpDir, err := os.MkdirTemp("", "cups-web-wm-apply-*") |
| 81 | + if err != nil { |
| 82 | + return "", nil, fmt.Errorf("create output temp dir: %w", err) |
| 83 | + } |
| 84 | + outCleanup := func() { os.RemoveAll(tmpDir) } |
| 85 | + |
| 86 | + outPath := filepath.Join(tmpDir, "watermarked.pdf") |
| 87 | + |
| 88 | + conf := model.NewDefaultConfiguration() |
| 89 | + conf.ValidationMode = model.ValidationRelaxed |
| 90 | + |
| 91 | + if err := api.AddWatermarksFile(inputPath, outPath, nil, wm, conf); err != nil { |
| 92 | + outCleanup() |
| 93 | + return "", nil, fmt.Errorf("apply watermark: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return outPath, outCleanup, nil |
| 97 | +} |
0 commit comments