From 485b7e6bee6f3fa0ab9aa19647da838c482e3803 Mon Sep 17 00:00:00 2001 From: Rico Brase Date: Thu, 8 Feb 2024 00:28:58 +0100 Subject: [PATCH] Fix Acroforms - setting an option to false will still apply the flag (#1496) * Add an option check to acroform mixin _resolveFlags(). This resolves #1495. * Reworked fix to remove flags from options, if they are falsey. * Added unit test for ignoring false flags in acroforms * Added entry for removing false flags in changelog. --- CHANGELOG.md | 1 + lib/mixins/acroform.js | 4 +++- tests/unit/acroform.spec.js | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2700583a..0a186e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix for line breaks in list items (#1486) - Fix for soft hyphen not being replaced by visible hyphen if necessary (#457) - Optimize output files by ignoring identity transforms +- Fix for Acroforms - setting an option to false will still apply the flag (#1495) ### [v0.14.0] - 2023-11-09 diff --git a/lib/mixins/acroform.js b/lib/mixins/acroform.js index f2ce118b..9249aa4e 100644 --- a/lib/mixins/acroform.js +++ b/lib/mixins/acroform.js @@ -299,7 +299,9 @@ export default { let result = 0; Object.keys(options).forEach(key => { if (FIELD_FLAGS[key]) { - result |= FIELD_FLAGS[key]; + if (options[key]) { + result |= FIELD_FLAGS[key]; + } delete options[key]; } }); diff --git a/tests/unit/acroform.spec.js b/tests/unit/acroform.spec.js index 4b39e28e..c75e464e 100644 --- a/tests/unit/acroform.spec.js +++ b/tests/unit/acroform.spec.js @@ -212,6 +212,33 @@ describe('acroform', () => { expect(docData).toContainChunk(expected); }); + test('false flags should be ignored', () => { + const expectedDoc = new PDFDocument({ + info: { CreationDate: new Date(Date.UTC(2018, 1, 1)) } + }); + expectedDoc.initForm(); + const expectedDocData = logData(expectedDoc); + let emptyOpts = { + align: 'center' + }; + expectedDoc.formText('flags', 20, 20, 50, 20, emptyOpts); + + doc.initForm(); + const docData = logData(doc); + let opts = { + required: false, + noExport: false, + readOnly: false, + align: 'center', + multiline: false, + password: false, + noSpell: false + }; + doc.formText('flags', 20, 20, 50, 20, opts); + + expect(docData).toContainChunk(expectedDocData); + }); + test('font size', () => { const expected = [ '11 0 obj',