Skip to content

Commit

Permalink
Merge branch 'AnkurGel-image_tiles' - image tile feature by @AnkurGel
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaxray committed Dec 27, 2022
2 parents 336e2c5 + 1a5cba3 commit 4206ecc
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 23 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Highlights -
- Extreamly fast!
- Stretching watermark image to height or weight proportionately
- Options to adjust position, opacity, rotation of image
- Placeholder for text watermark
- Tile image watermark all over the page
- Free and open source

## Install
Expand Down Expand Up @@ -56,7 +58,17 @@ markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" -Wo 0.3
# stretch full with of page at page bottom
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --scale-width --offset-y=-10
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" -wy -10
```

# Scale the image to desired percentage
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --scale=30

# Add image as tiles all over the page
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --tiles

# Add image as tiles with interleaved spacing
markpdf "path/to/source.pdf" "img/logo.png" "path/to/output.pdf" --tiles --spacing=20
``


### Text watermarking

Expand All @@ -83,11 +95,9 @@ The following placeholder can be used in text watermark:

```bash
# Using placeholders in text watermark
markpdf "path/to/083.pdf" "File: {{.Filename}} Page {{.Page}} of {{.Pages}}" "path/to/voucher_083.pdf" --position=10,-10
markpdf "path/to/083.pdf" "File: {{.Filename}} Page {{.Page}} of {{.Pages}}" "path/to/voucher_083.pdf" -x -20 -y 30
```

_Note: This (placeholder) feature will be available in upcoming release. If you want to use it right now, please build from the `master` branch._

#### Allowed font identifiers

Currently the following font names are supported:
Expand All @@ -100,7 +110,7 @@ Currently the following font names are supported:
- **Specifying Colors**: write them as 6 or 3 digit hexadecilal as used in CSS, without the #

- `--color`, `--font` and `--font-size` flag has no impact for Image watermarking
- `--scale-*` and `--opacity` flag has no impact for Text watermarking
- `--scale-*`, `--tiles` and `--opacity` flag has no impact for Text watermarking
- Negative offset will set content positioning from opposite side (right for offsetX and botom from offsetY)
- Text with opacity is not supported at this moment. Instead, you can [create a transperent background PNG image](http://www.picturetopeople.org/text_generator/others/transparent/transparent-text-generator.html) with your text and then use it for watermarking.

Expand All @@ -114,10 +124,10 @@ Currently the following font names are supported:
✅ Configure image rotation angle
✅ Options to Stretch watermark to page width or height, proportionately
✅ Options to Stretch watermark to page width or height at the middle of page
◻️ Tile Image all over the page
Tile Image all over the page
✅ Render text on every page
✅ Configure text color, style and font
◻️ Configure text opacity
Configure text opacity
✅ Configure text rotation angle
✅ Text placement by offset
✅ Put text at page center
Expand Down
13 changes: 11 additions & 2 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
env GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o builds/markpdf_darwin-amd64
env GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o builds/markpdf_darwin-amd64
upx builds/markpdf_darwin-amd64

env GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o builds/markpdf_darwin-arm64
# upx is not working properly for Mac M1 Build :(

env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o builds/markpdf_linux-amd64
upx builds/markpdf_linux-amd64

env GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o builds/markpdf_linux-arm64
upx builds/markpdf_linux-arm64

env GOOS=windows GOARCH=386 go build -ldflags="-s -w" -o builds/markpdf_windows-386.exe
upx builds/markpdf_*
upx builds/markpdf_windows-386.exe
12 changes: 10 additions & 2 deletions img_watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
)

func drawImage(watermarkImg *creator.Image, c *creator.Creator) {
watermarkImg.SetPos(offsetX, offsetY)
watermarkImg.SetOpacity(opacity)
watermarkImg.SetAngle(angle)
if tiles {
repeatTiles(watermarkImg, c)
return
}
watermarkImg.SetPos(offsetX, offsetY)
_ = c.Draw(watermarkImg)
}

Expand All @@ -20,9 +24,13 @@ func adjustImagePosition(watermarkImg *creator.Image, c *creator.Creator) {


if scaleImage != 100 {
debugInfo(fmt.Sprintf("Scaling to %v", scaleImage))
debugInfo(fmt.Sprintf("Scaling to %v%%", scaleImage))
watermarkImg.ScaleToHeight(scaleImage * watermarkImg.Width() / 100)
}
if tiles {
offsetX, offsetY = 0, 0
return
}
if scaleWCenter {
watermarkImg.ScaleToWidth(c.Context().PageWidth)
offsetX = 0
Expand Down
26 changes: 15 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package main

import (
"fmt"
"os"
"path/filepath"
"text/template"

flag "github.com/ogier/pflag"
unicommon "github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/creator"
pdf "github.com/unidoc/unidoc/pdf/model"
"os"
"path/filepath"
"text/template"
)

var offsetX, offsetY, scaleImage, fontSize float64
var scaleH, scaleW, scaleHCenter, scaleWCenter, center, verbose, version bool
var offsetX, offsetY, scaleImage, fontSize, spacing float64
var scaleH, scaleW, scaleHCenter, scaleWCenter, center, tiles, verbose, version bool
var opacity, angle float64
var font, color string

Expand All @@ -37,18 +38,21 @@ func init() {
flag.Float64VarP(&opacity, "opacity", "o", 0.5, "Opacity of watermark. float between 0 to 1.")
flag.Float64VarP(&angle, "angle", "a", 0, "Angle of rotation. between 0 to 360, counter clock-wise.")

flag.BoolVarP(&tiles, "tiles", "t", false, "Repeat watermark as tiles on page. All offsets will be ignored.")
flag.Float64VarP(&spacing, "spacing", "z", 0, "Repeat watermark as tiles on page. All offsets will be ignored.")

flag.BoolVarP(&verbose, "verbose", "v", false, "Display debug information.")
flag.BoolVarP(&version, "version", "V", false, "Display Version information.")

flag.Usage = func() {
fmt.Println("markpdf <source> <watermark> <output> [options...]")
fmt.Println("Usages: markpdf <source> <watermark> <output> [options...]")
fmt.Println("<source> and <output> should be path to a PDF file and <watermark> can be a text or path of an image.")
fmt.Println("text <watermark> can be used with the following variable:")
fmt.Println("{{.Page}} current page number")
fmt.Println("{{.Pages}} total page numbers")
fmt.Println("{{.Filename}} source file name")
fmt.Println("Example: markpdf \"path/to/083.pdf\" \"img/logo.png\" \"path/to/voucher_083.pdf\" --position=10,-10 --opacity=0.4")
fmt.Println("")
fmt.Println("Example: markpdf \"path/to/083.pdf\" \"img/logo.png\" \"path/to/voucher_083.pdf\" -x 10 -y -30 --opacity=0.5")
fmt.Println("Example: markpdf \"path/to/083.pdf\" \"img/logo.png\" \"path/to/tmp_voucher_083.pdf\" --tiles --spacing=50 --opacity=0.2")
fmt.Println("Example: markpdf \"path/to/083.pdf\" \"GreatCompanyName\" \"path/to/voucher_083.pdf\" -cf times_bold_italic")
fmt.Println("Example: markpdf \"path/to/083.pdf\" \"File: {{.Filename}} Page {{.Page}} of {{.Pages}}\" \"path/to/voucher_083.pdf\" --position=10,-10 --opacity=0.4")
fmt.Println("")
fmt.Println("Available Options: ")
flag.PrintDefaults()
}
Expand Down
12 changes: 11 additions & 1 deletion text_watermark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ func drawText(p *creator.Paragraph, c *creator.Creator) {
p.SetColor(creator.ColorRGBFromHex("#" + color))
p.SetAngle(angle)

// Encountering problem with tiles and text watermark. Contributions welcome.
//if tiles {
// repeatTiles(p, c)
// return
//}

_ = c.Draw(p)
}

func adjustTextPosition(p *creator.Paragraph, c *creator.Creator) {
p.SetTextAlignment(creator.TextAlignmentLeft)
p.SetEnableWrap(false)

if center {
if tiles {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)
offsetX, offsetY = 0, 0
} else if center {
p.SetWidth(p.Width()) // Not working without setting it manually
p.SetTextAlignment(creator.TextAlignmentCenter)

Expand Down
36 changes: 36 additions & 0 deletions tiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"github.com/unidoc/unidoc/pdf/creator"
"math"
)

// Watermarkable is a common interface for watermarkable image or paragraph
type Watermarkable interface {
creator.VectorDrawable
SetPos(x, y float64)
}

func repeatTiles(watermark Watermarkable, c *creator.Creator) {
w := watermark.Width()
h := watermark.Height()
pw := c.Context().PageWidth
ph := c.Context().PageHeight

nw := math.Ceil(pw / w)
nh := math.Ceil(ph / h)

debugInfo(fmt.Sprintf("Settings tiles of %v x %v", nw, nh))
for i := 0; i < int(nw); i++ {
x := w * float64(i)
for j := 0; j < int(nh); j++ {
y := h * float64(j)
watermark.SetPos(x + spacing * float64(i), y + spacing * float64(j))
err := c.Draw(watermark)
if err != nil {
fatalIfError(err, fmt.Sprintf("Error %s", err))
}
}
}
}

0 comments on commit 4206ecc

Please sign in to comment.