Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF watermark from Reader #472

Closed
vsenko opened this issue May 17, 2022 · 5 comments
Closed

PDF watermark from Reader #472

vsenko opened this issue May 17, 2022 · 5 comments
Assignees

Comments

@vsenko
Copy link

vsenko commented May 17, 2022

Looks like currently it's impossible to create PDF watermarks via Reader (or byte slice, etc.), only from file:

otherCtx, err := ReadFile(wm.FileName, NewDefaultConfiguration())

It would be really handy to add such a feature. Furthermore it's already there for Image watermarks:

wm.image, wm.width, wm.height, err = CreateImageResource(ctx.XRefTable, wm.Image, false, false)

@hhrutter
Copy link
Collaborator

hhrutter commented May 21, 2022

Hello!

I am not quite sure what you are looking for.
Please explain your specific usecase.

In case you are looking for something similar to:
api.ImageWatermarkForReader(r io.Reader, desc string, onTop, update bool, u pdfcpu.DisplayUnit)

maybe:
api.PDFWatermarkForReader(r io.Reader, desc string, onTop, update bool, u pdfcpu.DisplayUnit)

then that can't be done.

A PDF resource for stamping/watermarking is a combination of some content stream bytes, its resources and a bounding box whereas an Image resource can be represented by a simple byte slice.

On top of that pdfcpu supports multi-stamps where each page of the input PDF gets stamped with a different page of the PDF stamp source file. See more here: https://pdfcpu.io/core/stamp

@vsenko
Copy link
Author

vsenko commented May 23, 2022

Excuse me for being that brief, consider the following usecase: the watermark is generated on-the-fly for each incoming PDF from some metadata, thus it's inconvenient to save the watermark beforehand to the file. The code that generates the watermark is on another process/host.

Thank you for identifying the problem with io.Reader approach, but maybe we can go with io.ReadSeeker:

api.PDFWatermarkForReader(r io.ReadSeeker, desc string, onTop, update bool, u pdfcpu.DisplayUnit)

@vsenko
Copy link
Author

vsenko commented May 23, 2022

Another option (to keep the interface more consistent) is to copy data from io.Reader into io.ReadSeeker under the hood, but it will require twice more memory during the copy.

@hhrutter
Copy link
Collaborator

hhrutter commented May 27, 2022

Generating a pdfcpu PDF watermark somewhere in order to use it later on for stamping is a valid use case but can't be done easily.

Like already expained above, a PDF watermark is a non trivial object consisting of some content bytes, its resources and some metadata. Sure, we could write out this object/blob as a file, but this is not practical because of its required resources, especially when some embedded user fonts are involved. A much better alternative is to to render the PDF watermark to an image but that's not an option right now.

Switching from io.Reader to an io.ReadSeeker doesn't change anything AFAIC.

I need to think more about this because I like the use case.

@hhrutter
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants