Skip to content

Commit

Permalink
feat: pass creation and modification dates (#2539)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura authored Jan 22, 2024
1 parent cfd050c commit fb5273d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-roses-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@react-pdf/render': minor
'@react-pdf/types': minor
---

feat: add creation and modification dates
4 changes: 4 additions & 0 deletions packages/render/src/operations/addMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ const addMetadata = (ctx, doc) => {
const keywords = props.keywords || null;
const creator = props.creator ?? 'react-pdf';
const producer = props.producer ?? 'react-pdf';
const creationDate = props.creationDate || new Date();
const modificationDate = props.modificationDate || null;

setProp('Title', title);
setProp('Author', author);
setProp('Subject', subject);
setProp('Keywords', keywords);
setProp('Creator', creator);
setProp('Producer', producer);
setProp('CreationDate', creationDate);
setProp('ModificationDate', modificationDate);
};

export default addMetadata;
27 changes: 27 additions & 0 deletions packages/render/tests/operations/addMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,31 @@ describe('operations addMetadata', () => {

expect(ctx.info.Producer).toBe('test');
});

test('should add creationDate metadata if provided', () => {
const ctx = createCTX();
const doc = { type: P.Document, props: { creationDate: 'test' } };

addMetadata(ctx, doc);

expect(ctx.info.CreationDate).toBe('test');
});

test('should not add modificationDate metadata if none provided', () => {
const ctx = createCTX();
const doc = { type: P.Document };

addMetadata(ctx, doc);

expect(ctx.info.ModificationDate).toBeUndefined();
});

test('should add modificationDate metadata if provided', () => {
const ctx = createCTX();
const doc = { type: P.Document, props: { modificationDate: 'test' } };

addMetadata(ctx, doc);

expect(ctx.info.ModificationDate).toBe('test');
});
});
2 changes: 2 additions & 0 deletions packages/types/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ interface DocumentProps {
keywords?: string;
creator?: string;
producer?: string;
creationDate?: Date;
modificationDate?: Date;
pageLayout?: PageLayout;
pageMode?: PageMode;
}
Expand Down

0 comments on commit fb5273d

Please sign in to comment.