From 50937a3539720e92b0090ac0d6c191fd48eb1612 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 12 Sep 2023 13:09:58 +0200 Subject: [PATCH] Ensure that the entire PDF document is loaded *before* we begin saving it When I started looking at PR 16938 it occurred to me that some of the new structTree-methods are synchronously accessing certain dictionary-data (not used during "normal" structTree-parsing), which may not be generally safe since everything in a dictionary could be a reference (and the relevant data may not have been loaded yet). Rather than suggesting that we make all those new methods even more asynchronous, to me the overall simplest and safest solution is to ensure that the *entire* PDF document has been loaded *before* we begin saving it. In practice this shouldn't really affect "performance" of saving noticeably, since it's always depended on the entire PDF document being downloaded. Finally note that with the exception of the PDF document possibly not having been fully downloaded when saving is triggered, all other "global" document properties are pretty much guaranteed to already be available at this point. --- src/core/worker.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index 9230652ce30fc..d03c2b95770c2 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -535,19 +535,21 @@ class WorkerMessageHandler { handler.on( "SaveDocument", async function ({ isPureXfa, numPages, annotationStorage, filename }) { - const promises = [ + const globalPromises = [ pdfManager.requestLoadedStream(), pdfManager.ensureCatalog("acroForm"), pdfManager.ensureCatalog("acroFormRef"), pdfManager.ensureDoc("startXRef"), + pdfManager.ensureDoc("xref"), pdfManager.ensureDoc("linearization"), ]; + const promises = []; const newAnnotationsByPage = !isPureXfa ? getNewAnnotationsMap(annotationStorage) : null; - - const xref = await pdfManager.ensureDoc("xref"); + const [stream, acroForm, acroFormRef, startXRef, xref, linearization] = + await Promise.all(globalPromises); if (newAnnotationsByPage) { const imagePromises = AnnotationFactory.generateImages( @@ -587,14 +589,7 @@ class WorkerMessageHandler { } } - return Promise.all(promises).then(function ([ - stream, - acroForm, - acroFormRef, - startXRef, - linearization, - ...refs - ]) { + return Promise.all(promises).then(refs => { let newRefs = []; let xfaData = null; if (isPureXfa) {