Skip to content

Commit

Permalink
fix: pdf/a compliance of attachments (#1562)
Browse files Browse the repository at this point in the history
* fix: pdf/a compliance of attachments

* fixed tests

* update docs

---------

Co-authored-by: Libor M. <[email protected]>
  • Loading branch information
florianbepunkt and liborm85 authored Dec 14, 2024
1 parent ffbbee5 commit bc59dd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ There are a few other options for `doc.file`:
* `hidden` - if true, do not show file in the list of embedded files
* `creationDate` - override the date and time the file was created
* `modifiedDate` - override the date and time the file was last updated
* `relationship` - relationship between the PDF document and its attached file. Can be 'Alternative', 'Data', 'Source', 'Supplement' or 'Unspecified'.

If you are attaching a file from your file system, creationDate and modifiedDate will be set to the source file's creationDate and modifiedDate.

Expand Down
10 changes: 10 additions & 0 deletions lib/mixins/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export default {
* * options.hidden: if true, do not add attachment to EmbeddedFiles dictionary. Useful for file attachment annotations
* * options.creationDate: override creation date
* * options.modifiedDate: override modified date
* * options.relationship: Relationship between the PDF document and its attached file. Can be 'Alternative', 'Data', 'Source', 'Supplement' or 'Unspecified'.
* @returns filespec reference
*/
file(src, options = {}) {
options.name = options.name || src;
options.relationship = options.relationship || 'Unspecified';

const refBody = {
Type: 'EmbeddedFile',
Expand Down Expand Up @@ -85,6 +87,7 @@ export default {
// add filespec for embedded file
const fileSpecBody = {
Type: 'Filespec',
AFRelationship: options.relationship,
F: new String(options.name),
EF: { F: ref },
UF: new String(options.name)
Expand All @@ -99,6 +102,13 @@ export default {
this.addNamedEmbeddedFile(options.name, filespec);
}

// Add file to the catalogue to be PDF/A3 compliant
if (this._root.data.AF) {
this._root.data.AF.push(filespec);
} else {
this._root.data.AF = [filespec];
}

return filespec;
}
};
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/attachments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('file', () => {
`9 0 obj`,
`<<
/Type /Filespec
/AFRelationship /Unspecified
/F (file.txt)
/EF <<
/F 8 0 R
Expand Down Expand Up @@ -112,6 +113,7 @@ describe('file', () => {
`9 0 obj`,
`<<
/Type /Filespec
/AFRelationship /Unspecified
/F (file.txt)
/EF <<
/F 8 0 R
Expand Down

0 comments on commit bc59dd2

Please sign in to comment.