Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.77k stars 647 forks source link

Can't call translateContent multiple times per page #1420

Open ammulder opened 1 year ago

ammulder commented 1 year ago

What were you trying to do?

I am trying to generate a PDF to print on Avery 5163 labels -- 4"x2" labels with 10 per page (2 across and 5 down).

For each of the 10 labels on the page I would like to translateContent and then draw the content and then resetPosition, so there would be 10 translateContent calls per page.

How did you attempt to do it?

I would like my code to look something like this:

translateContent(...)
...draw first label...
resetPosition()
translateContent(...)
...draw second label...
resetPosition()

OK, in reality it uses for loops on row and column but you get the idea.

What actually happened?

Only the last of the 10 labels rendered on the page.

What did you expect to happen?

I expected all 10 labels to render on the page.

How can we reproduce the issue?

See above

Version

1.17.1

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

My guess is that the problem is that translateContent includes this:

this.node.wrapContentStreams(startRef, endRef);

It looks like that's wrapping all the content streams on the node instead of just the current one. So I think every time I call translateContent it shifts all the content already in the page, so the earlier labels get their positions shifted many times and end up outside the visible area of the page, and only the last label to be drawn is only shifted once and ends up in the correct position.

ZhetoN commented 10 months ago

any progress on this?