pdf 添加跨域功能 修复pdf 页码和高亮没启用bug

This commit is contained in:
高雄
2026-04-15 14:40:41 +08:00
parent 171762d676
commit ee2a27501b
2 changed files with 79 additions and 44 deletions

View File

@@ -23083,7 +23083,7 @@ initCom(PDFViewerApplication);
}
{
const HOSTED_VIEWER_ORIGINS = new Set(["null", "http://mozilla.github.io", "https://mozilla.github.io"]);
var validateFileURL = function (file) {
var validateFileURL = function (file) {
if (!file) {
return;
}
@@ -23091,6 +23091,7 @@ initCom(PDFViewerApplication);
if (HOSTED_VIEWER_ORIGINS.has(viewerOrigin)) {
return;
}
/* 注释掉跨域检查
const fileOrigin = URL.parse(file, window.location)?.origin;
if (fileOrigin === viewerOrigin) {
return;
@@ -23100,7 +23101,8 @@ initCom(PDFViewerApplication);
message: ex.message
});
throw ex;
};
*/
};
var onFileInputChange = function (evt) {
if (this.pdfViewer?.isInPresentationMode) {
return;

View File

@@ -1,55 +1,88 @@
<!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 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);
viewerUrl += "&disablepresentationmode=${pdfPresentationModeDisable}";
viewerUrl += "&disableopenfile=${pdfOpenFileDisable}";
viewerUrl += "&disableprint=${pdfPrintDisable}";
viewerUrl += "&disabledownload=${pdfDownloadDisable}";
viewerUrl += "&disablebookmark=${pdfBookmarkDisable}";
viewerUrl += "&disableediting=${pdfDisableEditing}";
viewerUrl += "&pdfhighlightall=${highlightall}";
viewerUrl += "&watermarktxt=${watermarkTxt}";
viewerUrl += "#page=${page?js_string}";
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 () {
if (typeof initWaterMark === 'function') {
initWaterMark();
}
};
</script>
</body>
</html>