-
Notifications
You must be signed in to change notification settings - Fork 3.2k
pdf 添加跨域功能 修复pdf 页码和高亮没启用bug #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gaoxingzaq
wants to merge
5
commits into
kekingcn:master
Choose a base branch
from
gaoxingzaq:pdfjs2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,90 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <html lang="zh-CN"> | ||
| <head> | ||
| <meta charset="utf-8"/> | ||
| <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0"> | ||
| <title>PDF预览</title> | ||
| <#include "*/commonHeader.ftl"> | ||
| <script src="js/base64.min.js" type="text/javascript"></script> | ||
| <style> | ||
| /* 简单全屏布局,无滚动条 */ | ||
| html, body { | ||
| margin: 0; | ||
| padding: 0; | ||
| height: 100%; | ||
| overflow: hidden; | ||
| } | ||
| iframe { | ||
| width: 100%; | ||
| height: 100%; | ||
| border: none; | ||
| display: block; | ||
| } | ||
| .img-preview { | ||
| position: fixed; | ||
| bottom: 20px; | ||
| right: 20px; | ||
| cursor: pointer; | ||
| z-index: 999; | ||
| width: 48px; | ||
| height: 48px; | ||
| } | ||
| </style> | ||
| </head> | ||
|
|
||
| <body> | ||
|
|
||
| <#if pdfUrl?contains("http://") || pdfUrl?contains("https://")> | ||
| <#assign finalUrl="${pdfUrl}"> | ||
| <#else> | ||
| <#assign finalUrl="${baseUrl}${pdfUrl}"> | ||
| </#if> | ||
| <iframe src="" width="100%" frameborder="0"></iframe> | ||
|
|
||
| <iframe id="pdfFrame" src="about:blank"></iframe> | ||
|
|
||
| <#if "false" == switchDisabled> | ||
| <img src="images/jpg.svg" width="48" height="48" style="position: fixed; cursor: pointer; top: 40%; right: 48px; z-index: 999;" alt="使用图片预览" title="使用图片预览" onclick="goForImage()"/> | ||
| <img class="img-preview" src="images/jpg.svg" alt="使用图片预览" title="使用图片预览" onclick="goForImage()"/> | ||
| </#if> | ||
| </body> | ||
|
|
||
| <script type="text/javascript"> | ||
| var url = '${finalUrl}'; | ||
| var kkagent = '${kkagent}'; | ||
| var url = '${finalUrl}'; | ||
| var kkagent = '${kkagent}'; | ||
| var baseUrl = '${baseUrl}'.endsWith('/') ? '${baseUrl}' : '${baseUrl}' + '/'; | ||
| if (kkagent === 'true' || !url.startsWith(baseUrl)) { | ||
| url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url))+ "&key=${kkkey}"; | ||
| } | ||
| document.getElementsByTagName('iframe')[0].src = "${baseUrl}pdfjs/web/viewer.html?file=" + encodeURIComponent(url) + "&disablepresentationmode=${pdfPresentationModeDisable}&disableopenfile=${pdfOpenFileDisable}&disableprint=${pdfPrintDisable}&disabledownload=${pdfDownloadDisable}&disablebookmark=${pdfBookmarkDisable}&disableediting=${pdfDisableEditing}#page=1&pagemode=thumbs"; | ||
| document.getElementsByTagName('iframe')[0].height = document.documentElement.clientHeight - 10; | ||
| /** | ||
| * 页面变化调整高度 | ||
| */ | ||
| window.onresize = function () { | ||
| var fm = document.getElementsByTagName("iframe")[0]; | ||
| fm.height = window.document.documentElement.clientHeight - 10; | ||
| url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url)) + "&key=${kkkey}"; | ||
| } | ||
| var viewerUrl = baseUrl + "pdfjs/web/viewer.html?file=" + encodeURIComponent(url); | ||
| var watermarkEncoded = encodeURIComponent('${watermarkTxt?js_string}'); | ||
| var highlightEncoded = encodeURIComponent('${highlightall?js_string}'); | ||
| viewerUrl += "&disablepresentationmode=${pdfPresentationModeDisable}"; | ||
| viewerUrl += "&disableopenfile=${pdfOpenFileDisable}"; | ||
| viewerUrl += "&disableprint=${pdfPrintDisable}"; | ||
| viewerUrl += "&disabledownload=${pdfDownloadDisable}"; | ||
| viewerUrl += "&disablebookmark=${pdfBookmarkDisable}"; | ||
| viewerUrl += "&disableediting=${pdfDisableEditing}"; | ||
| viewerUrl += "&watermarktxt=" + watermarkEncoded; | ||
| viewerUrl += "&pdfhighlightall=" + highlightEncoded; | ||
| viewerUrl += "#page=${page}"; // ?c 确保数字不包含千位分隔符 | ||
| viewerUrl += "&pagemode=thumbs"; | ||
| var iframe = document.getElementById('pdfFrame'); | ||
| iframe.src = viewerUrl; | ||
|
|
||
| // 图片预览切换 | ||
| function goForImage() { | ||
| var url = window.location.href | ||
| if (url.indexOf("officePreviewType=pdf") != -1) { | ||
| url = url.replace("officePreviewType=pdf", "officePreviewType=image"); | ||
| var href = window.location.href; | ||
| if (href.indexOf("officePreviewType=pdf") !== -1) { | ||
| href = href.replace("officePreviewType=pdf", "officePreviewType=image"); | ||
| } else { | ||
| url = url + "&officePreviewType=image"; | ||
| href += (href.indexOf('?') === -1 ? '?' : '&') + "officePreviewType=image"; | ||
| } | ||
| window.location.href = url; | ||
| window.location.href = href; | ||
| } | ||
|
|
||
| /*初始化水印*/ | ||
| // 水印初始化(保持原有逻辑) | ||
| window.onload = function () { | ||
| initWaterMark(); | ||
| } | ||
| if (typeof initWaterMark === 'function') { | ||
| initWaterMark(); | ||
| } | ||
| }; | ||
| </script> | ||
| </html> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里把 pdf.js 的同源校验整体去掉了,但 kkFileView 自己其实已经有跨域代理路径:
pdf.ftl会把非本地 URL 改写到/getCorsFile,后端入口在OnlinePreviewController#getCorsFile。也就是说这次需求更像是修 kkFileView 的预览入口,而不是全局放开 vendoredviewer.html的跨域限制。现在这个改法会扩大直接访问/pdfjs/web/viewer.html时的行为面,而且后续同步 pdf.js 版本也更难维护。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这块是为了配合 ${kkagent} 如果接入服务器支持了 跨域功能 就跳过KK反代