Generate the document's ID on demand instead of the class constructor
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.
⚠️ No Changeset found
Latest commit: 6619076a7373fbcab1694646a758c299f7e68720
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
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?