Merge pull request #14005 from Snuffleupagus/issue-13997

[GENERIC viewer] Always show the Download-buttons, to allow saving of forms (issue 13997)
This commit is contained in:
Tim van der Meij 2021-09-18 16:31:15 +02:00 committed by GitHub
commit 2375318a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View File

@ -252,6 +252,7 @@ const PDFViewerApplication = {
isViewerEmbedded: window.parent !== window,
url: "",
baseUrl: "",
_downloadUrl: "",
externalServices: DefaultExternalServices,
_boundEvents: Object.create(null),
documentInfo: null,
@ -749,9 +750,13 @@ const PDFViewerApplication = {
});
},
setTitleUsingUrl(url = "") {
setTitleUsingUrl(url = "", downloadUrl = null) {
this.url = url;
this.baseUrl = url.split("#")[0];
if (downloadUrl) {
this._downloadUrl =
downloadUrl === url ? this.baseUrl : downloadUrl.split("#")[0];
}
let title = getPdfFilenameFromUrl(url, "");
if (!title) {
try {
@ -779,6 +784,16 @@ const PDFViewerApplication = {
return this._contentDispositionFilename || getPdfFilenameFromUrl(this.url);
},
/**
* @private
*/
_hideViewBookmark() {
// URL does not reflect proper document location - hiding some buttons.
const { toolbar, secondaryToolbar } = this.appConfig;
toolbar.viewBookmark.hidden = true;
secondaryToolbar.viewBookmarkButton.hidden = true;
},
/**
* @private
*/
@ -799,6 +814,7 @@ const PDFViewerApplication = {
*/
async close() {
this._unblockDocumentLoadEvent();
this._hideViewBookmark();
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
const { container } = this.appConfig.errorWrapper;
@ -841,6 +857,7 @@ const PDFViewerApplication = {
this.downloadComplete = false;
this.url = "";
this.baseUrl = "";
this._downloadUrl = "";
this.documentInfo = null;
this.metadata = null;
this._contentDispositionFilename = null;
@ -889,13 +906,13 @@ const PDFViewerApplication = {
const parameters = Object.create(null);
if (typeof file === "string") {
// URL
this.setTitleUsingUrl(file);
this.setTitleUsingUrl(file, /* downloadUrl = */ file);
parameters.url = file;
} else if (file && "byteLength" in file) {
// ArrayBuffer
parameters.data = file;
} else if (file.url && file.originalUrl) {
this.setTitleUsingUrl(file.originalUrl);
this.setTitleUsingUrl(file.originalUrl, /* downloadUrl = */ file.url);
parameters.url = file.url;
}
// Set the necessary API parameters, using the available options.
@ -971,7 +988,7 @@ const PDFViewerApplication = {
},
async download({ sourceEventType = "download" } = {}) {
const url = this.baseUrl,
const url = this._downloadUrl,
filename = this._docFilename;
try {
this._ensureDownloadComplete();
@ -994,7 +1011,7 @@ const PDFViewerApplication = {
this._saveInProgress = true;
await this.pdfScriptingManager.dispatchWillSave();
const url = this.baseUrl,
const url = this._downloadUrl,
filename = this._docFilename;
try {
this._ensureDownloadComplete();
@ -2267,13 +2284,17 @@ function webViewerOpenFileViaURL(file) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (file) {
PDFViewerApplication.open(file);
} else {
PDFViewerApplication._hideViewBookmark();
}
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
PDFViewerApplication.setTitleUsingUrl(file);
PDFViewerApplication.setTitleUsingUrl(file, /* downloadUrl = */ file);
PDFViewerApplication.initPassiveLoading();
} else {
if (file) {
throw new Error("Not implemented: webViewerOpenFileViaURL");
} else {
PDFViewerApplication._hideViewBookmark();
}
}
}
@ -2493,13 +2514,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
};
fileReader.readAsArrayBuffer(file);
}
// URL does not reflect proper document location - hiding some icons.
const appConfig = PDFViewerApplication.appConfig;
appConfig.toolbar.viewBookmark.hidden = true;
appConfig.secondaryToolbar.viewBookmarkButton.hidden = true;
appConfig.toolbar.download.hidden = true;
appConfig.secondaryToolbar.downloadButton.hidden = true;
};
webViewerOpenFile = function (evt) {

View File

@ -49,6 +49,7 @@ class DownloadManager {
downloadUrl(url, filename) {
if (!createValidAbsoluteUrl(url, "http://example.com")) {
console.error(`downloadUrl - not a valid URL: ${url}`);
return; // restricted/invalid URL
}
download(url + "#pdfjs.action=download", filename);