Throw from addNamedEmbeddedFile when no ref is given - #1772
Merged
blikblum merged 1 commit intoAug 22, 2026
Merged
Conversation
Calling it without a ref wrote the literal token `undefined` into the /EmbeddedFiles name tree, producing a PDF that does not parse. doc.file() already rejects a missing src; this makes the lower-level route agree.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #1771, as you asked — the throw on its own.
lib/tree.jsisuntouched, so the
PDFTree.toStringfilter you were less inclined to merge isnot in here.
The bug
undefinedis not a PDF object, so the document does not parse. The caller askedfor an attachment under a name and got a broken file with no signal.
Why throw, of the three you listed
doc.file()already refuses the same thing:addNamedEmbeddedFileis the lower-level route to the same place, so the two nowagree. Silent drop is the option I would argue against hardest — a valid PDF with
nothing attached and no error is harder to debug than a stack trace.
In
addNamedEmbeddedFilerather thanPDFTree.add, becauseaddis shared with/Dests,/JavaScriptandParentTree, which only ever receive values thelibrary builds itself.
Scope
!refrather than=== undefined, matching theif (!src)guard infile().That also catches
null, which is the more awkward of the two: it serialises tothe PDF
nullkeyword, so the file parses and you get a silently emptyattachment rather than a loud broken one. Happy to narrow it if you would rather.
The guard sits before the lazy
/EmbeddedFilesinit, so a rejected call does notleave an empty name tree behind in the catalog.
nameis not validated. A bad key serialises to(undefined), which parses, andthe same applies to
addNamedDestinationandaddNamedJavaScript— fixing ithere alone would make the three siblings inconsistent. Separate change.
The one caller inside the library,
lib/mixins/attachments.js:102, passes afilespec
file()has just created, andfile()throws before it can returnanything falsy — so the library's own path cannot reach the new guard.
Marked breaking, though anyone hitting this today is shipping a PDF that does not
parse.
Tests
2 added.
throws when the ref is missingfails on master. The second registers afilespec created with
hidden: trueand passes either way — a guard that thenormal hand-registration path still works, not evidence for the change.
Unit suite 396/396.