Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/src/main/resources/static/pdfjs/web/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,8 @@
<!-- editorUndoBar -->
</div>
<!-- outerContainer -->
<script type="text/javascript" src="/js/jquery-3.6.1.min.js"></script>
<script type="text/javascript" src="/js/pdfwatermark.js"></script>
<script type="text/javascript" src="../../js/jquery-3.6.1.min.js"></script>
<script type="text/javascript" src="../../js/pdfwatermark.js"></script>
<div id="printContainer"></div>
</body>
</html>
38 changes: 20 additions & 18 deletions server/src/main/resources/static/pdfjs/web/viewer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23083,24 +23083,26 @@ initCom(PDFViewerApplication);
}
{
const HOSTED_VIEWER_ORIGINS = new Set(["null", "http://mozilla.github.io", "https://mozilla.github.io"]);
var validateFileURL = function (file) {
if (!file) {
return;
}
const viewerOrigin = URL.parse(window.location)?.origin || "null";
if (HOSTED_VIEWER_ORIGINS.has(viewerOrigin)) {
return;
}
const fileOrigin = URL.parse(file, window.location)?.origin;
if (fileOrigin === viewerOrigin) {
return;
}
const ex = new Error("file origin does not match viewer's");
PDFViewerApplication._documentError("pdfjs-loading-error", {
message: ex.message
});
throw ex;
};
var validateFileURL = function (file) {
Copy link
Copy Markdown
Contributor

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 的预览入口,而不是全局放开 vendored viewer.html 的跨域限制。现在这个改法会扩大直接访问 /pdfjs/web/viewer.html 时的行为面,而且后续同步 pdf.js 版本也更难维护。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块是为了配合 ${kkagent} 如果接入服务器支持了 跨域功能 就跳过KK反代

if (!file) {
return;
}
const viewerOrigin = URL.parse(window.location)?.origin || "null";
if (HOSTED_VIEWER_ORIGINS.has(viewerOrigin)) {
return;
}
/* 注释掉跨域检查
const fileOrigin = URL.parse(file, window.location)?.origin;
if (fileOrigin === viewerOrigin) {
return;
}
const ex = new Error("file origin does not match viewer's");
PDFViewerApplication._documentError("pdfjs-loading-error", {
message: ex.message
});
throw ex;
*/
};
var onFileInputChange = function (evt) {
if (this.pdfViewer?.isInPresentationMode) {
return;
Expand Down
87 changes: 61 additions & 26 deletions server/src/main/resources/web/pdf.ftl
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>
Loading