Skip to content
New issue

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

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

Already on GitHub? # to your account

#1033 broke image insertion for loaded documents #1075

Closed
4 tasks done
rafaelgomesxyz opened this issue Nov 4, 2021 · 2 comments · Fixed by #1078
Closed
4 tasks done

#1033 broke image insertion for loaded documents #1075

rafaelgomesxyz opened this issue Nov 4, 2021 · 2 comments · Fixed by #1078

Comments

@rafaelgomesxyz
Copy link

What were you trying to do?

I was trying to add more images to a page in an existing document.

Why were you trying to do this?

Because I wanted to add more images.

How did you attempt to do it?

I added the new images with PDFPage.drawImage:

page.drawImage(image, { x, y, width, height });

What actually happened?

The new images replaced the previous images that were on the page.

What did you expect to happen?

The new images should not have replaced the previous images.

How can we reproduce the issue?

  1. Create or load a document
  2. Add an image to the first page of the document
  3. Save the document
  4. Load the document again
  5. Add another image to the first page of the document
  6. Save the document again
  7. Now both images in the first page of the document are the same

This repo contains a reproduceable example: https://github.com/rafaelgomesxyz/pdf-lib-image-bug

git clone https://github.com/rafaelgomesxyz/pdf-lib-image-bug.git
cd pdf-lib-image-bug
node index.js

See the document-1.pdf and document-2.pdf. On document-2.pdf, the first image was replaced by the second image.

Version

1.17.0

What environment are you running pdf-lib in?

Node

Required Reading

Additional Notes

I'm pretty sure this happens because SimpleRNG is always initiated with 1 (adeb734#diff-0f847354b81d20ae6094bfc27efa03e850d82939ebd49594d0c0b92b1b83be8cR71).

So I guess when you load the document again, it just generates the exact same keys for the images, and so any new images replace the previous images because they have the same key.

On v1.16.0 it's working correctly, because it gets the key from here: https://github.com/Hopding/pdf-lib/blob/master/src/utils/strings.ts#L34 And in this function it correctly generates a random key every time.

@Hopding
Copy link
Owner

Hopding commented Nov 5, 2021

@rafaelgomesxyz thanks for reporting this issue and providing a clear, reproducible description! I think you're right about the recent SimpleRNG change being the cause. We want to keep using a deterministic RNG, but in order to fix this bug, we'll need to check the generated key against the existing keys in the document to prevent collisions.

@rafaelgomesxyz
Copy link
Author

No problem!

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

Successfully merging a pull request may close this issue.

2 participants