Skip to content

Commit

Permalink
Merge pull request #474 from nextcloud/backport/472/stable21
Browse files Browse the repository at this point in the history
[stable21] Fix download & print view
  • Loading branch information
PVince81 authored Aug 20, 2021
2 parents a959f77 + 8d9285c commit acb2563
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 54 deletions.
27 changes: 24 additions & 3 deletions js/files_pdfviewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-main.js.map

Large diffs are not rendered by default.

27 changes: 24 additions & 3 deletions js/files_pdfviewer-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-public.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/files_pdfviewer-workersrc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-workersrc.js.map

Large diffs are not rendered by default.

53 changes: 12 additions & 41 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ window.addEventListener('DOMContentLoaded', function() {
isSecureViewerAvailable: isSecureViewerAvailable(),
})

// If we display a folder, we don't have anything more to do here
if (isPublicPage() && !isPdf()) {
logger.debug('But this is not a single pdf share')
return
}

// If we display a single PDF and we don't use the richdocument secureViewer
if (isPublicPage() && isPdf() && !isSecureViewerAvailable()) {
const page = location.hash.split('page=')[1] || 0
const contentElmt = document.getElementById('files-public-content')
Expand All @@ -42,7 +49,11 @@ window.addEventListener('DOMContentLoaded', function() {

const sharingToken = sharingTokenElmt.value
const downloadUrl = generateUrl('/s/{token}/download', { token: sharingToken })
const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}#page={page}', { downloadUrl, page })
const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}&canDownload={canDownload}#page={page}', {
canDownload: canDownload() ? 1 : 0,
downloadUrl,
page,
})

// Create viewer frame
const viewerNode = document.createElement('iframe')
Expand All @@ -59,46 +70,6 @@ window.addEventListener('DOMContentLoaded', function() {
} else {
logger.error('Unable to inject the PDF Viewer')
}

// When pdf viewer is loaded
addEventListener('load', function() {
// If we forbid download, prevent interaction
if (!canDownload()) {
const pdfViewer = viewerNode.contentDocument.querySelector('.pdfViewer')
const PDFViewerApplication = viewerNode.contentWindow.PDFViewerApplication

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, printing and user interaction disabled')
}
})
} else {
logger.error('But this does not appear to be a public page')
}
Expand Down
4 changes: 3 additions & 1 deletion src/views/PDFView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

<script>
import { generateUrl } from '@nextcloud/router'
import canDownload from '../utils/canDownload'
export default {
name: 'PDFView',
computed: {
iframeSrc() {
return generateUrl('/apps/files_pdfviewer/?file={file}', {
return generateUrl('/apps/files_pdfviewer/?file={file}&canDownload={canDownload}', {
canDownload: canDownload() ? 1 : 0,
file: this.davPath,
})
},
Expand Down
43 changes: 43 additions & 0 deletions src/workersrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import redirectIfNotIframe from './utils/redirectIfNotIframe'
// Checks if the page is displayed in an iframe. If not redirect to /.
redirectIfNotIframe()

// Retrieve the canDownload 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 canDownload = urlParams.get('canDownload')

// When "PDFViewerApplication.webViewerInitialized" is executed (once
// "PDFViewerApplication.initialize" is done) it opens the PDF file via URL,
// which requires the PDFViewerApplication to be properly configured, so the
Expand All @@ -45,6 +51,43 @@ function initializeCustomPDFViewerApplication() {
PDFViewerApplicationOptions.set('cMapUrl', document.getElementsByTagName('head')[0].getAttribute('data-cmapurl'))
PDFViewerApplicationOptions.set('enablePermissions', true)

if (canDownload === '0') {
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())
}

Expand Down

0 comments on commit acb2563

Please sign in to comment.