Skip to content

AddWatermark returns error on PDF with empty page #843

@atlekbai

Description

@atlekbai

The function github.com/pdfcpu/pdfcpu/pkg/api.AddWatermarks returns pdfcpu: page without content.

The pdf file, which is being added as watermark, contains pdf pages with no content at all.

Expected behavior

The pdf is successfully being added as watermark, including empty pages. The empty pages might be placed in the beginning, in the middle or in the end of the pdf file.

Actual behavior

The function returns an error.

Environment

  • OS: macOS Sonoma 14.3
  • Go: 1.21.5

Code to reproduce

go.mod

go 1.21.5

require github.com/pdfcpu/pdfcpu v0.7.0

require (
	github.com/hhrutter/lzw v1.0.0 // indirect
	github.com/hhrutter/tiff v1.0.1 // indirect
	github.com/mattn/go-runewidth v0.0.15 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	github.com/rivo/uniseg v0.4.4 // indirect
	golang.org/x/image v0.12.0 // indirect
	golang.org/x/text v0.13.0 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
)

main.go

package main

import (
	"bytes"
	_ "embed"
	"log"

	"github.com/pdfcpu/pdfcpu/pkg/api"
	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
)

var (
	//go:embed blank.pdf
	blankBytes []byte

	//go:embed empty_page.pdf
	pdfBytes []byte
)

func main() {
	var (
		buf           bytes.Buffer
		desc          = "offset: 10 0 ,rot:0, scale:0.8 rel"
		fileName      = "test.pdf"
		selectedPages = "1-2"
	)

	wm, err := pdfcpu.ParsePDFWatermarkDetails(fileName, desc, false, types.POINTS)
	if err != nil {
		log.Fatalf("error on pdfcpu.ParsePDFWatermarkDetails: %v", err)
	}

	wm.PDF = bytes.NewReader(pdfBytes)

	// here returns error:
	// 	pdfcpu: page without content
	if err := api.AddWatermarks(
		bytes.NewReader(blankBytes),
		&buf,
		[]string{selectedPages},
		wm,
		model.NewDefaultConfiguration(),
	); err != nil {
		log.Fatalf("error on api.AddWatermarks: %v", err)
	}
}

PDF files:

Possible solution

The actual error is being returned from this line - pkg/pdfcpu/model/xreftable.go:1783:

	if len(bb) == 0 {
		return nil, ErrNoContent
	}

I tried to remove these lines and the function AddWatermarks works as expected.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions