From 33fdec139224cbcba4eeb248068d48422f31b455 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Thu, 20 Jul 2023 17:05:52 +0200 Subject: [PATCH] Don't replace Acroform dictionary if nothing has changed when saving (bug 1844572) --- src/core/worker.js | 6 +++++- src/core/writer.js | 2 +- src/core/xref.js | 8 +++++++- test/pdfs/bug1844572.pdf.link | 2 ++ test/test_manifest.json | 23 +++++++++++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test/pdfs/bug1844572.pdf.link diff --git a/src/core/worker.js b/src/core/worker.js index a606c74f3..04dd77e80 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -544,6 +544,7 @@ class WorkerMessageHandler { pdfManager.ensureCatalog("acroForm"), pdfManager.ensureCatalog("acroFormRef"), pdfManager.ensureDoc("startXRef"), + pdfManager.ensureDoc("linearization"), ]; const newAnnotationsByPage = !isPureXfa @@ -595,6 +596,7 @@ class WorkerMessageHandler { acroForm, acroFormRef, startXRef, + linearization, ...refs ]) { let newRefs = []; @@ -656,7 +658,9 @@ class WorkerMessageHandler { infoRef: xref.trailer.getRaw("Info") || null, info: infoObj, fileIds: xref.trailer.get("ID") || null, - startXRef: xref.lastXRefStreamPos ?? startXRef, + startXRef: linearization + ? startXRef + : xref.lastXRefStreamPos ?? startXRef, filename, }; } diff --git a/src/core/writer.js b/src/core/writer.js index 776d7ce35..009ea4cce 100644 --- a/src/core/writer.js +++ b/src/core/writer.js @@ -231,7 +231,7 @@ async function updateAcroform({ warn("XFA - Cannot save it"); } - if (!needAppearances && (!hasXfa || !xfaDatasetsRef)) { + if (!needAppearances && (!hasXfa || !xfaDatasetsRef || hasXfaDatasetsEntry)) { return; } diff --git a/src/core/xref.js b/src/core/xref.js index 1194725b8..6dfb2d770 100644 --- a/src/core/xref.js +++ b/src/core/xref.js @@ -33,6 +33,8 @@ import { BaseStream } from "./base_stream.js"; import { CipherTransformFactory } from "./crypto.js"; class XRef { + #firstXRefStmPos = null; + constructor(stream, pdfManager) { this.stream = stream; this.pdfManager = pdfManager; @@ -705,6 +707,7 @@ class XRef { // (possible infinite recursion) this._xrefStms.add(obj); this.startXRefQueue.push(obj); + this.#firstXRefStmPos ??= obj; } } else if (Number.isInteger(obj)) { // Parse in-stream XRef @@ -754,7 +757,10 @@ class XRef { } get lastXRefStreamPos() { - return this._xrefStms.size > 0 ? Math.max(...this._xrefStms) : null; + return ( + this.#firstXRefStmPos ?? + (this._xrefStms.size > 0 ? Math.max(...this._xrefStms) : null) + ); } getEntry(i) { diff --git a/test/pdfs/bug1844572.pdf.link b/test/pdfs/bug1844572.pdf.link new file mode 100644 index 000000000..ea3ed9cc8 --- /dev/null +++ b/test/pdfs/bug1844572.pdf.link @@ -0,0 +1,2 @@ +https://bugzilla.mozilla.org/attachment.cgi?id=9344818 + diff --git a/test/test_manifest.json b/test/test_manifest.json index 32112565b..e9a9d854a 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -8008,5 +8008,28 @@ "rotation": 0 } } + }, + { + "id": "bug1844572-save-print", + "file": "pdfs/bug1844572.pdf", + "md5": "22a96775e884131d59c05fa92e0a42f6", + "rounds": 1, + "type": "eq", + "link": true, + "save": true, + "print": true, + "annotationStorage": { + "pdfjs_internal_editor_0": { + "annotationType": 3, + "color": [0, 0, 0], + "fontSize": 10, + "value": "Hello World", + "pageIndex": 0, + "rect": [ + 115.80000000000001, 325.0679969482421, 171.3, 342.5679999999999 + ], + "rotation": 0 + } + } } ]