From 7c5e9cf8be1c833ecaafbe963959755a53b6556e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 15 Jun 2024 17:39:59 +0200 Subject: [PATCH 1/2] Ensure that saving, in the viewer, works for partially loaded documents Currently saving a modified PDF document may fail *intermittently*, if it's triggered before the entire document has been downloaded. When saving was originally added we only supported forms, and such PDF documents are usually small/simple enough for this issue to be difficult to trigger. However, with editing-support now available as well it's possible to modify much larger documents and this issue thus becomes easier to trigger. One way to reproduce this issue *consistently* is to: - Open http://localhost:8888/web/viewer.html?file=/test/pdfs/pdf.pdf#disableHistory=true&disableStream=true&disableAutoFetch=true - Add an annotation on the first page, it doesn't matter what kind. - Save the document. - Open the resulting document, and notice that with the `master` branch the annotation is missing. --- web/app.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/web/app.js b/web/app.js index 927d7143ede44..d8dd9e95389b9 100644 --- a/web/app.js +++ b/web/app.js @@ -1109,8 +1109,6 @@ const PDFViewerApplication = { await this.pdfScriptingManager.dispatchWillSave(); try { - this._ensureDownloadComplete(); - const data = await this.pdfDocument.saveDocument(); this.downloadManager.download( data, @@ -1119,8 +1117,7 @@ const PDFViewerApplication = { options ); } catch (reason) { - // When the PDF document isn't ready, or the PDF file is still - // downloading, simply fallback to a "regular" download. + // When the PDF document isn't ready, fallback to a "regular" download. console.error(`Error when saving the document: ${reason.message}`); await this.download(options); } finally { From f4912db2ae58cec0c2e9b6e302f0ae6e6c15d252 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 15 Jun 2024 17:45:38 +0200 Subject: [PATCH 2/2] Remove the `_ensureDownloadComplete` helper method in `web/app.js` After the previous commit this method has only a single call-site, hence we can inline the needed part of that check directly in `PDFViewerApplication.download` instead. --- web/app.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/web/app.js b/web/app.js index d8dd9e95389b9..8bcb86f20d69e 100644 --- a/web/app.js +++ b/web/app.js @@ -1073,22 +1073,12 @@ const PDFViewerApplication = { ); }, - /** - * @private - */ - _ensureDownloadComplete() { - if (this.pdfDocument && this.downloadComplete) { - return; - } - throw new Error("PDF document not downloaded."); - }, - async download(options = {}) { let data; try { - this._ensureDownloadComplete(); - - data = await this.pdfDocument.getData(); + if (this.downloadComplete) { + data = await this.pdfDocument.getData(); + } } catch { // When the PDF document isn't ready, or the PDF file is still // downloading, simply download using the URL.