Skip to content

Commit 05d16ab

Browse files
skjnldsvbackportbot[bot]
authored andcommitted
Fix download & print view
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent a959f77 commit 05d16ab

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

src/public.js

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ window.addEventListener('DOMContentLoaded', function() {
3434
isSecureViewerAvailable: isSecureViewerAvailable(),
3535
})
3636

37+
// If we display a folder, we don't have anything more to do here
38+
if (isPublicPage() && !isPdf()) {
39+
logger.debug('But this is not a single pdf share')
40+
return
41+
}
42+
43+
// If we display a single PDF and we don't use the richdocument secureViewer
3744
if (isPublicPage() && isPdf() && !isSecureViewerAvailable()) {
3845
const page = location.hash.split('page=')[1] || 0
3946
const contentElmt = document.getElementById('files-public-content')
@@ -42,7 +49,11 @@ window.addEventListener('DOMContentLoaded', function() {
4249

4350
const sharingToken = sharingTokenElmt.value
4451
const downloadUrl = generateUrl('/s/{token}/download', { token: sharingToken })
45-
const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}#page={page}', { downloadUrl, page })
52+
const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}&canDownload={canDownload}#page={page}', {
53+
canDownload: canDownload() ? 1 : 0,
54+
downloadUrl,
55+
page,
56+
})
4657

4758
// Create viewer frame
4859
const viewerNode = document.createElement('iframe')
@@ -59,46 +70,6 @@ window.addEventListener('DOMContentLoaded', function() {
5970
} else {
6071
logger.error('Unable to inject the PDF Viewer')
6172
}
62-
63-
// When pdf viewer is loaded
64-
addEventListener('load', function() {
65-
// If we forbid download, prevent interaction
66-
if (!canDownload()) {
67-
const pdfViewer = viewerNode.contentDocument.querySelector('.pdfViewer')
68-
const PDFViewerApplication = viewerNode.contentWindow.PDFViewerApplication
69-
70-
if (pdfViewer) {
71-
pdfViewer.classList.add('disabledTextSelection')
72-
}
73-
74-
if (PDFViewerApplication) {
75-
// Disable download function when downloads are hidden, as even if the
76-
// buttons in the UI are hidden the download could still be triggered
77-
// with Ctrl|Meta+S.
78-
PDFViewerApplication.download = function() {
79-
}
80-
81-
// Disable printing service when downloads are hidden, as even if the
82-
// buttons in the UI are hidden the printing could still be triggered
83-
// with Ctrl|Meta+P.
84-
// Abuse the "supportsPrinting" parameter, which signals that the
85-
// browser does not fully support printing, to make PDFViewer disable
86-
// the printing service.
87-
// "supportsPrinting" is a getter function, so it needs to be deleted
88-
// before replacing it with a simple value.
89-
delete PDFViewerApplication.supportsPrinting
90-
PDFViewerApplication.supportsPrinting = false
91-
92-
// When printing is not supported a warning is shown by the default
93-
// "beforePrint" function when trying to print. That function needs to
94-
// be replaced with an empty one to prevent that warning to be shown.
95-
PDFViewerApplication.beforePrint = function() {
96-
}
97-
}
98-
99-
logger.info('Download, printing and user interaction disabled')
100-
}
101-
})
10273
} else {
10374
logger.error('But this does not appear to be a public page')
10475
}

src/views/PDFView.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626

2727
<script>
2828
import { generateUrl } from '@nextcloud/router'
29+
import canDownload from '../utils/canDownload'
2930
3031
export default {
3132
name: 'PDFView',
3233
3334
computed: {
3435
iframeSrc() {
35-
return generateUrl('/apps/files_pdfviewer/?file={file}', {
36+
return generateUrl('/apps/files_pdfviewer/?file={file}&canDownload={canDownload}', {
37+
canDownload: canDownload() ? 1 : 0,
3638
file: this.davPath,
3739
})
3840
},

src/workersrc.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ import redirectIfNotIframe from './utils/redirectIfNotIframe'
2828
// Checks if the page is displayed in an iframe. If not redirect to /.
2929
redirectIfNotIframe()
3030

31+
// Retrieve the canDownload from the url, this is
32+
// the most easy way to pass the prop to this iframe
33+
const queryString = window.location.search
34+
const urlParams = new URLSearchParams(queryString)
35+
const canDownload = urlParams.get('canDownload')
36+
3137
// When "PDFViewerApplication.webViewerInitialized" is executed (once
3238
// "PDFViewerApplication.initialize" is done) it opens the PDF file via URL,
3339
// which requires the PDFViewerApplication to be properly configured, so the
@@ -45,6 +51,43 @@ function initializeCustomPDFViewerApplication() {
4551
PDFViewerApplicationOptions.set('cMapUrl', document.getElementsByTagName('head')[0].getAttribute('data-cmapurl'))
4652
PDFViewerApplicationOptions.set('enablePermissions', true)
4753

54+
if (canDownload === '0') {
55+
const pdfViewer = window.document.querySelector('.pdfViewer')
56+
57+
if (pdfViewer) {
58+
pdfViewer.classList.add('disabledTextSelection')
59+
}
60+
61+
if (PDFViewerApplication) {
62+
// Disable download function when downloads are hidden, as even if the
63+
// buttons in the UI are hidden the download could still be triggered
64+
// with Ctrl|Meta+S.
65+
PDFViewerApplication.download = function() {
66+
}
67+
68+
// Disable printing service when downloads are hidden, as even if the
69+
// buttons in the UI are hidden the printing could still be triggered
70+
// with Ctrl|Meta+P.
71+
// Abuse the "supportsPrinting" parameter, which signals that the
72+
// browser does not fully support printing, to make PDFViewer disable
73+
// the printing service.
74+
// "supportsPrinting" is a getter function, so it needs to be deleted
75+
// before replacing it with a simple value.
76+
delete PDFViewerApplication.supportsPrinting
77+
PDFViewerApplication.supportsPrinting = false
78+
79+
// When printing is not supported a warning is shown by the default
80+
// "beforePrint" function when trying to print. That function needs to
81+
// be replaced with an empty one to prevent that warning to be shown.
82+
PDFViewerApplication.beforePrint = function() {
83+
}
84+
}
85+
86+
logger.info('Download, print and user interaction disabled')
87+
} else {
88+
logger.info('Download and print available')
89+
}
90+
4891
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
4992
}
5093

0 commit comments

Comments
 (0)