Skip to content

Commit

Permalink
Move PDFViewerApplication setup from helper script to Vue component
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Feb 5, 2025
1 parent b50d09b commit 44bffe8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 52 deletions.
43 changes: 41 additions & 2 deletions src/views/PDFView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default {

computed: {
iframeSrc() {
return generateUrl('/apps/files_pdfviewer/?file={file}&hideDownload={hideDownload}', {
hideDownload: hideDownload() ? 1 : 0,
return generateUrl('/apps/files_pdfviewer/?file={file}', {
file: this.source ?? this.davPath,
})
},
Expand Down Expand Up @@ -184,6 +183,46 @@ export default {
this.getDownloadElement().removeAttribute('disabled')
}
})

if (hideDownload()) {
const pdfViewer = this.getIframeDocument().querySelector('.pdfViewer')

if (pdfViewer) {
pdfViewer.classList.add('disabledTextSelection')
}

// Disable download function when downloads are hidden, as even
// if the buttons in the UI are hidden the download could still
// be triggered with Ctrl|Meta+S.
this.PDFViewerApplication.download = () => {
}

// Disable printing service when downloads are hidden, as even
// if the buttons in the UI are hidden the printing could still
// be triggered with Ctrl|Meta+P.
// Abuse the "supportsPrinting" parameter, which signals that
// the browser does not fully support printing, to make
// PDFViewer disable the printing service.
// "supportsPrinting" is a getter function, so it needs to be
// deleted before replacing it with a simple value.
delete this.PDFViewerApplication.supportsPrinting
this.PDFViewerApplication.supportsPrinting = false

// When printing is not supported a warning is shown by the
// default "beforePrint" function when trying to print. That
// function needs to be replaced with an empty one to prevent
// that warning to be shown.
this.PDFViewerApplication.beforePrint = () => {
}

logger.info('Download, print and user interaction disabled')
} else {
logger.info('Download and print available')
}

const PDFViewerApplicationOptions = this.$refs.iframe.contentWindow.PDFViewerApplicationOptions

logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
},

handleWebviewerloaded() {
Expand Down
50 changes: 0 additions & 50 deletions src/workersrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import logger from './services/logger.js'
import redirectIfNotIframe from './utils/redirectIfNotIframe.js'

// Checks if the page is displayed in an iframe. If not redirect to /.
redirectIfNotIframe()

// Retrieve the hideDownload from the url, this is
// the most easy way to pass the prop to this iframe
const queryString = window.location.search
const urlParams = new URLSearchParams(queryString)
const hideDownload = urlParams.get('hideDownload')

function initializeCustomPDFViewerApplication() {
if (hideDownload === '1') {
const pdfViewer = window.document.querySelector('.pdfViewer')

if (pdfViewer) {
pdfViewer.classList.add('disabledTextSelection')
}

if (PDFViewerApplication) {
// Disable download function when downloads are hidden, as even if the
// buttons in the UI are hidden the download could still be triggered
// with Ctrl|Meta+S.
PDFViewerApplication.download = function() {
}

// Disable printing service when downloads are hidden, as even if the
// buttons in the UI are hidden the printing could still be triggered
// with Ctrl|Meta+P.
// Abuse the "supportsPrinting" parameter, which signals that the
// browser does not fully support printing, to make PDFViewer disable
// the printing service.
// "supportsPrinting" is a getter function, so it needs to be deleted
// before replacing it with a simple value.
delete PDFViewerApplication.supportsPrinting
PDFViewerApplication.supportsPrinting = false

// When printing is not supported a warning is shown by the default
// "beforePrint" function when trying to print. That function needs to
// be replaced with an empty one to prevent that warning to be shown.
PDFViewerApplication.beforePrint = function() {
}
}

logger.info('Download, print and user interaction disabled')
} else {
logger.info('Download and print available')
}

logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
}

document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)

0 comments on commit 44bffe8

Please sign in to comment.