Skip to content

Commit

Permalink
Add hide download and print flag
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Nov 21, 2023
1 parent 6bed754 commit 8918e8d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
48 changes: 48 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ const PDFViewerApplication = {
_touchInfo: null,
_isCtrlKeyDown: false,
_nimbusDataPromise: null,
canDownload: true,
canPrint: true,

// Called once when the document is loaded.
async initialize(appConfig) {
Expand Down Expand Up @@ -256,6 +258,9 @@ const PDFViewerApplication = {
this.bindWindowEvents();

this._initializedCapability.resolve();

this.canDownload = appConfig.canDownload;
this.canPrint = appConfig.canPrint;
},

/**
Expand Down Expand Up @@ -690,6 +695,13 @@ const PDFViewerApplication = {
} else {
throw new Error("Not implemented: run");
}

if (!this.canDownload) {
this._hideDownloadButtons();
}
if (!this.canPrint) {
this._hidePrintButtons();
}
},

get initialized() {
Expand Down Expand Up @@ -875,6 +887,24 @@ const PDFViewerApplication = {
}
},

/**
* @private
*/
_hidePrintButtons() {
const { toolbar, secondaryToolbar } = this.appConfig;
toolbar?.print.classList.add("hidden");
secondaryToolbar?.printButton.classList.add("hidden");
},

/**
* @private
*/
_hideDownloadButtons() {
const { toolbar, secondaryToolbar } = this.appConfig;
toolbar?.download.classList.add("hidden");
secondaryToolbar?.downloadButton.classList.add("hidden");
},

/**
* Closes opened PDF document.
* @returns {Promise} - Returns the promise, which is resolved when all
Expand Down Expand Up @@ -1092,6 +1122,9 @@ const PDFViewerApplication = {
},

downloadOrSave(options = {}) {
if (!this.canDownload) {
return;
}
if (this.pdfDocument?.annotationStorage.size > 0) {
this.save(options);
} else {
Expand Down Expand Up @@ -1743,6 +1776,9 @@ const PDFViewerApplication = {
},

beforePrint() {
if (!this.canPrint) {
return;
}
this._printAnnotationStoragePromise = this.pdfScriptingManager
.dispatchWillPrint()
.catch(() => {
Expand Down Expand Up @@ -1806,6 +1842,9 @@ const PDFViewerApplication = {
},

afterPrint() {
if (!this.canPrint) {
return;
}
if (this._printAnnotationStoragePromise) {
this._printAnnotationStoragePromise.then(() => {
this.pdfScriptingManager.dispatchDidPrint();
Expand Down Expand Up @@ -1835,6 +1874,9 @@ const PDFViewerApplication = {
},

triggerPrinting() {
if (!this.canPrint) {
return;
}
if (!this.supportsPrinting) {
return;
}
Expand Down Expand Up @@ -2420,9 +2462,15 @@ function webViewerSwitchAnnotationEditorParams(evt) {
PDFViewerApplication.pdfViewer.annotationEditorParams = evt;
}
function webViewerPrint() {
if (!PDFViewerApplication.canPrint) {
return;
}
PDFViewerApplication.triggerPrinting();
}
function webViewerDownload() {
if (!PDFViewerApplication.canDownload) {
return;
}
PDFViewerApplication.downloadOrSave();
}
function webViewerOpenInExternalApp() {
Expand Down
4 changes: 4 additions & 0 deletions web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ function getViewerConfiguration() {
? document.getElementById("fileInput")
: null,
debuggerScriptPath: "./debugger.mjs",
canDownload:
new URLSearchParams(window.location.search).get("download") !== "0",
canPrint:
new URLSearchParams(window.location.search).get("download") !== "0",
};
}

Expand Down

0 comments on commit 8918e8d

Please sign in to comment.