pdfcpu / pdfcpu

A PDF processor written in Go.

Home Page:http://pdfcpu.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

addPageWatermark: ctx.PageDict(pageNr, consolidateRes) returns no error, but nil dictionary

comzine opened this issue · comments

OS: macOS 14.4.1
pdfcpu 0.8.0

I use api.AddWatermarksSliceMapFile(...) for printing stamps inside a pdf file. Unfortunately I get panics from

func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {
	if h == nil {
		panic(plainError("assignment to entry in nil map"))
	}

With debugging I found out, that the following is probably the reason for that:

func addPageWatermark(ctx *model.Context, pageNr int, wm model.Watermark) error {
	if pageNr > ctx.PageCount {
		return errors.Errorf("pdfcpu: invalid page number: %d", pageNr)
	}

	if log.DebugEnabled() {
		log.Debug.Printf("addPageWatermark page:%d\n", pageNr)
	}

	if wm.Update {
		if log.DebugEnabled() {
			log.Debug.Println("Updating")
		}
		if _, err := removePageWatermark(ctx, pageNr); err != nil {
			return err
		}
	}

	consolidateRes := false
	d, pageIndRef, inhPAttrs, err := ctx.PageDict(pageNr, consolidateRes)
	if err != nil {
		return err
	}

d is nil, but err is nil too.

Can you share a test file in order to reproduce this?
There is a reason for returning nil for d when calling PageDict , maybe you pass in an invalid page number?