Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [BREAKING CHANGE] Remove the virtual file system (`pdfkit/virtual-fs`). Browser builds no longer depend on `fs`: pass a `Uint8Array`, `ArrayBuffer` or data URL to `registerFont`, `image` and `file` instead of a path
- [BREAKING CHANGE] Restrict AcroForm options to documented mappings and explicit escape hatches.
- [BREAKING CHANGE] Stop automatically uppercasing annotation option keys.
- [BREAKING CHANGE] Throw from `addNamedEmbeddedFile` when no ref is given, instead of writing an unparseable `undefined` token into the `/EmbeddedFiles` name tree
- Do not mutate options passed to `doc.annotate()` and its convenience methods (link, note, strike, lineAnnotation, rectAnnotation, ellipseAnnotation, textAnnotation, fileAnnotation)
- Persist font options when adding a new page. Fixes #1739
- Use `Uint8Array` instead of Node's `Buffer` internally
Expand Down
4 changes: 4 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ class PDFDocument extends Readable {
}

addNamedEmbeddedFile(name, ref) {
if (!ref) {
throw new Error(`No ref specified for embedded file ${name}`);
}

if (!this._root.data.Names.data.EmbeddedFiles) {
// disabling /Limits for this tree fixes attachments not showing in Adobe Reader
this._root.data.Names.data.EmbeddedFiles = new PDFNameTree({
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/attachments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,44 @@ describe('file', () => {
(file1.txt) 10 0 R
]
>>
>>`,
]);
});

test('throws when the ref is missing', () => {
expect(() =>
document.addNamedEmbeddedFile('phantom.txt', undefined),
).toThrow('No ref specified for embedded file phantom.txt');

expect(() => document.addNamedEmbeddedFile('phantom.txt', null)).toThrow(
'No ref specified for embedded file phantom.txt',
);
});

test('registers a filespec created with the hidden option', () => {
const docData = logData(document);

const filespec = document.file(Buffer.from('example text'), {
name: 'file.txt',
creationDate: date,
modifiedDate: date,
hidden: true,
});
document.addNamedEmbeddedFile('file.txt', filespec);
document.end();

expect(docData).toContainChunk([
`2 0 obj`,
`<<
/Dests <<
/Names [
]
>>
/EmbeddedFiles <<
/Names [
(file.txt) 9 0 R
]
>>
>>`,
]);
});
Expand Down
Loading