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

Generate the document's ID on demand instead of the class constructor #2814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

markusylisiurunen
Copy link

Solves #2813.

Referring to #2813, subsequent <Document /> renders produces a PDF file with non-deterministic /ID even though the creationDate and modificationDate props were set. If I managed to understand correctly, the ID is generated when the PDFDocument class was instantiated. However, some of the .info object's fields were updated from outside the class instance during the rendering process:

const setPDFMetadata = (target) => (key, value) => {
  if (value) target.info[key] = value;
};

const addMetadata = (ctx, doc) => {
  const setProp = setPDFMetadata(ctx);

  // ...
  const creationDate = props.creationDate || new Date();
  const modificationDate = props.modificationDate || null;

  // ...
  setProp('CreationDate', creationDate);
  setProp('ModificationDate', modificationDate);
};

However, the ID was generated based on the .info in the constructor and was not updated after the .info object was updated. This means that the ID is generated with the default CreationDate which is the current time.

// Initialize the metadata
this.info = {
  Producer: 'PDFKit',
  Creator: 'PDFKit',
  CreationDate: new Date()
};

if (this.options.info) {
  for (let key in this.options.info) {
    const val = this.options.info[key];
    this.info[key] = val;
  }
}

this._id = PDFSecurity.generateFileID(this.info);

This PR changes the behaviour so that the ID is not generated in the constructor but rather only when it is needed. Therefore, the modifications in the .info object should already be made at that point.

Copy link

changeset-bot bot commented Jul 10, 2024

⚠️ No Changeset found

Latest commit: 6619076

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@diegomura
Copy link
Owner

I was trying to unify our pdfkit fork with the real package, and this introduces a new change. Can we do this on react-pdf side by overriding the trailer?

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

Successfully merging this pull request may close these issues.

2 participants