Skip to content
Open
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
8 changes: 8 additions & 0 deletions lib/mixins/acroform.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ export default {
opts.Parent = opts.parent;
delete opts.parent;
}
// Drop entries with an `undefined` value so they are not serialized as the
// literal token `undefined` (e.g. `/SomeKey undefined`), which produces an
// invalid PDF object that strict parsers reject.
Object.keys(opts).forEach((key) => {
if (opts[key] === undefined) {
delete opts[key];
}
});
return opts;
},

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/acroform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ describe('acroform', () => {

expect(docData).toContainChunk(expectedDocData);
});

test('options with an undefined value are omitted', () => {
// A field created with an extra option set to `undefined` must serialize
// identically to one created without that option at all - the key must
// not leak into the dictionary as the literal token `undefined`, which is
// an invalid PDF object that strict parsers reject (issue #1735).
const expectedDoc = new PDFDocument({
info: { CreationDate: new Date(Date.UTC(2018, 1, 1)) },
});
expectedDoc.initForm();
const expectedDocData = logData(expectedDoc);
expectedDoc.formText('demofield', 20, 20, 50, 20, {});

doc.initForm();
const docData = logData(doc);
doc.formText('demofield', 20, 20, 50, 20, { CustomKey: undefined });

expect(docData.join('')).not.toMatch(/undefined/);
expect(docData).toContainChunk(expectedDocData);
});
});

test('flags', () => {
Expand Down