Changes for page PDF Viewer Macro

Last modified by Dongxu Lu on 2023/07/04 15:04

From version 1.1
edited by Dongxu Lu
on 2023/04/06 15:06
Change comment: Install extension [com.xwiki.pdfviewer:macro-pdfviewer-ui/2.4]
To version 2.1
edited by Dongxu Lu
on 2023/04/06 15:48
Change comment: Install extension [org.xwiki.contrib:xwiki-macro-pdfviewer/1.7]

Summary

Details

Page properties
Content
... ... @@ -1,1 +1,4 @@
1 +
2 +Sample PDF Viewer
3 +
1 1  {{pdfviewer file="pdftest.pdf" /}}
pdfviewer.zip
Author
... ... @@ -1,0 +1,1 @@
1 +xwiki:XWiki.DongxuLu
Size
... ... @@ -1,0 +1,1 @@
1 +2.5 MB
Content
XWiki.JavaScriptExtension[0]
Caching policy
... ... @@ -1,1 +1,0 @@
1 -long
Code
... ... @@ -1,48 +1,0 @@
1 -require(['jquery'], function ($) {
2 - $(function() {
3 - const iframe = document.getElementsByClassName('pdfviewer')[0];
4 - const initPermalinkButton = function (iframe) {
5 - let viewerFrame = $(iframe);
6 - let secondaryMenuButton = viewerFrame.contents().find('#secondaryToolbarToggle');
7 - // Triggered when clicking the permalink button.
8 - let copyPdfPermalink = function () {
9 - let fileName = new URL(viewerFrame.attr('src'), location.origin).searchParams.get('file');
10 - let pdfURL = new URL(fileName, location.origin).toString();
11 - navigator.clipboard.writeText(pdfURL).then(function() {
12 - // After upgrading the parent to 13.8, the Require plugin for translations should be used.
13 - // See https://extensions.xwiki.org/xwiki/bin/view/Extension/Localization/#HFromJavaScript
14 - new XWiki.widgets.Notification(
15 - $jsontool.serialize($services.localization.render('pdfviewer.viewer.notification.linkCopied')),
16 - 'done'
17 - );
18 - }, function() {
19 - new XWiki.widgets.Notification(
20 - $jsontool.serialize($services.localization.render('pdfviewer.viewer.notification.copyLinkFail')),
21 - 'error'
22 - );
23 - });
24 - // If the secondary menu is open, we need to close it / untoggle it.
25 - if (secondaryMenuButton.hasClass('toggled')) {
26 - secondaryMenuButton.click();
27 - }
28 - };
29 - viewerFrame.contents()
30 - .find('button.permalink')
31 - .attr('title', $jsontool.serialize($services.localization.render('pdfviewer.viewer.buttons.copyLink')))
32 - .on('click', copyPdfPermalink)
33 - .find('span')
34 - .text($jsontool.serialize($services.localization.render('pdfviewer.viewer.buttons.copyLink')))
35 - .removeAttr('data-l10n-id');
36 - };
37 -
38 - if (iframe.contentDocument.readyState === 'complete') {
39 - initPermalinkButton(iframe);
40 - } else {
41 - // We setTimeout to 0 because we want all the listeners of load event to finish executing before we run our
42 - // function. This will guarantee that the permalink button will be there when initPermalinkButton runs.
43 - iframe
44 - .contentWindow
45 - .addEventListener('load', function() { setTimeout(function () { initPermalinkButton(iframe); }, 0); });
46 - }
47 - });
48 -});
Parse content
... ... @@ -1,1 +1,0 @@
1 -Yes
Use this extension
... ... @@ -1,1 +1,0 @@
1 -onDemand
XWiki.WikiMacroClass[0]
Macro code
... ... @@ -1,0 +1,65 @@
1 +{{velocity}}
2 +#set($width = $context.macro.params.width)
3 +#set($height = $context.macro.params.height)
4 +#set($file = $context.macro.params.file)
5 +#set($docname = $context.macro.params.document)
6 +#set($asAuthor = $context.macro.params.asauthor && ($context.macro.params.asauthor == "true" || $context.macro.params.asauthor == "yes" || $context.macro.params.asauthor == "1"))
7 +#if(!$file)
8 + {{error}}$services.localization.render('pdfviewer.error.nofile'){{/error}}
9 +#else
10 + #if($file.startsWith("http://") || $file.startsWith("https://"))
11 + #set($url = $file)
12 + #else
13 + #if("$!docname" != "")
14 + #set($document = $xwiki.getDocument($docname))
15 + #else
16 + #set($document = $doc)
17 + #end
18 + #set($attachment = $document.getAttachment($file))
19 + #set($url = $NULL)
20 + #if($attachment)
21 + #set($url = $document.getAttachmentURL($file))
22 + #elseif ($asAuthor && $xwiki.hasProgrammingRights() && $xwiki.exists('XWiki.PDFViewerService'))
23 + #set($document = $xwiki.getDocumentAsAuthor($docname))
24 + #set($attachment = $document.getAttachment($file))
25 + #if($attachment)
26 + #set($digestFactory = $services.component.getInstance('org.xwiki.crypto.DigestFactory', 'SHA-1'))
27 + #set($encoder = $services.component.getInstance('org.xwiki.crypto.BinaryStringEncoder', 'URLBase64'))
28 + #if ($digestFactory && $encoder)
29 + #set($contentAuthor = $services.model.serialize($doc.contentAuthorReference, 'default'))
30 + #set($digest = $digestFactory.instance)
31 + #set($is = $digest.getInputStream($attachment.contentInputStream))
32 + #set($discard = $digest.getInputStream($attachment.contentInputStream).skip(1000))
33 + #set($discard = $is.close())
34 + #if ($xcontext.userReference)
35 + #set($discard = $digest.update($services.model.serialize($xcontext.userReference, 'default').getBytes('UTF-8')))
36 + #end
37 + #set($discard = $digest.update($contentAuthor.getBytes('UTF-8')))
38 + #set($digest = $encoder.encode($digest.digest))
39 + #set($url = $xwiki.getDocument("XWiki.PDFViewerService").getExternalURL('get', "reference=$escapetool.url($services.model.serialize($document.documentReference, 'default'))&filename=$escapetool.url($attachment.filename)&user=$escapetool.url($contentAuthor)&key=$digest"))
40 + #end
41 + #end
42 + #end
43 + #end
44 + #if ($url)
45 + #set($url = $escapetool.url($url))
46 + #set($pdfviewerurl = $xwiki.getDocument("XWiki.PDFViewerMacro").getAttachmentURL("pdfviewer.zip"))
47 + ## Attachment URL have query parameters for cache issue.
48 + #set($pdfviewerurl = $pdfviewerurl.split("\?")[0])
49 + {{html clean=false}}
50 + <div>
51 + <!--[if lt IE 10]>
52 + <div class="box warningmessage">PDF Viewer : Not compatible with IE < 10</div>
53 + <![endif]-->
54 + <![if !(lt IE 10)]>
55 + <iframe src="${pdfviewerurl}/pdf-viewer/web/viewer.html?file=${url}#locale=${context.language}" width="${width}" height="${height}"></iframe>
56 + <![endif]-->
57 + </div>
58 + {{/html}}
59 + #elseif (!$document || $document.isNew())
60 + {{error}}$services.localization.render('pdfviewer.error.nodocument', [$escapetool.html($escapetool.html($docname))]){{/error}}
61 + #else
62 + {{error}}$services.localization.render('pdfviewer.error.noattachment', [$escapetool.html($file), $escapetool.html($services.model.serialize($document.documentReference, 'default'))]){{/error}}
63 + #end
64 +#end
65 +{{/velocity}}
Macro content availability
... ... @@ -1,0 +1,1 @@
1 +No content
Macro description
... ... @@ -1,0 +1,1 @@
1 +PDF Viewer based on Mozilla pdf.js
Macro id
... ... @@ -1,0 +1,1 @@
1 +pdfviewer
Macro name
... ... @@ -1,0 +1,1 @@
1 +pdfviewer
Supports inline mode
... ... @@ -1,0 +1,1 @@
1 +No
Macro visibility
... ... @@ -1,0 +1,1 @@
1 +Current Wiki
XWiki.WikiMacroParameterClass[0]
Parameter description
... ... @@ -1,0 +1,1 @@
1 +File or URL path to the PDF file
Parameter mandatory
... ... @@ -1,0 +1,1 @@
1 +Yes
Parameter name
... ... @@ -1,0 +1,1 @@
1 +file
XWiki.WikiMacroParameterClass[1]
Parameter description
... ... @@ -1,0 +1,1 @@
1 +Reference of the document containing the file (not used if file is a URL)
Parameter name
... ... @@ -1,0 +1,1 @@
1 +document
XWiki.WikiMacroParameterClass[2]
Parameter default value
... ... @@ -1,0 +1,1 @@
1 +100%
Parameter name
... ... @@ -1,0 +1,1 @@
1 +width
XWiki.WikiMacroParameterClass[3]
Parameter default value
... ... @@ -1,0 +1,1 @@
1 +1000
Parameter name
... ... @@ -1,0 +1,1 @@
1 +height
XWiki.WikiMacroParameterClass[4]
Parameter default value
... ... @@ -1,0 +1,1 @@
1 +0
Parameter description
... ... @@ -1,0 +1,1 @@
1 +If this value is true (or 1 or yes) and the user has no access to the document to which the PDF file is attached, the PDF file could still be viewed on behalf of the author of the document containing the macro (if that author has access to the containing document obviously).
Parameter name
... ... @@ -1,0 +1,1 @@
1 +asauthor