2023-08-03 09:37:01 +02:00
|
|
|
import {
|
|
|
|
GlobalWorkerOptions,
|
|
|
|
PasswordResponses,
|
|
|
|
getDocument,
|
|
|
|
renderTextLayer,
|
|
|
|
} from "pdfjs-dist";
|
2019-06-28 05:22:08 +02:00
|
|
|
|
2023-08-03 09:37:01 +02:00
|
|
|
GlobalWorkerOptions.workerSrc = "/viewer/js/worker.js";
|
2021-11-21 21:39:19 +01:00
|
|
|
|
2019-06-28 05:22:08 +02:00
|
|
|
let pdfDoc = null;
|
|
|
|
let pageRendering = false;
|
|
|
|
let renderPending = false;
|
2020-04-04 18:14:38 +02:00
|
|
|
let renderPendingZoom = 0;
|
2023-03-17 23:03:17 +01:00
|
|
|
const canvas = document.getElementById("content");
|
2023-03-17 22:53:36 +01:00
|
|
|
const container = document.getElementById("container");
|
2017-11-14 18:29:10 +01:00
|
|
|
let orientationDegrees = 0;
|
2019-11-18 11:49:58 +01:00
|
|
|
let zoomRatio = 1;
|
2019-06-28 05:22:08 +02:00
|
|
|
let textLayerDiv = document.getElementById("text");
|
|
|
|
let task = null;
|
|
|
|
|
|
|
|
let newPageNumber = 0;
|
2019-11-18 11:49:58 +01:00
|
|
|
let newZoomRatio = 1;
|
2019-06-28 05:22:08 +02:00
|
|
|
let useRender;
|
|
|
|
|
|
|
|
const cache = [];
|
|
|
|
const maxCached = 6;
|
|
|
|
|
2023-03-18 14:36:23 +01:00
|
|
|
let isTextLayerVisible = false;
|
|
|
|
|
2019-06-28 05:22:08 +02:00
|
|
|
function maybeRenderNextPage() {
|
|
|
|
if (renderPending) {
|
|
|
|
pageRendering = false;
|
|
|
|
renderPending = false;
|
2020-04-04 17:31:16 +02:00
|
|
|
renderPage(channel.getPage(), renderPendingZoom, false);
|
2019-06-28 05:22:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleRenderingError(error) {
|
|
|
|
console.log("rendering error: " + error);
|
|
|
|
|
|
|
|
pageRendering = false;
|
|
|
|
maybeRenderNextPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
function doPrerender(pageNumber, prerenderTrigger) {
|
|
|
|
if (useRender) {
|
|
|
|
if (pageNumber + 1 <= pdfDoc.numPages) {
|
|
|
|
renderPage(pageNumber + 1, false, true, pageNumber);
|
|
|
|
} else if (pageNumber - 1 > 0) {
|
|
|
|
renderPage(pageNumber - 1, false, true, pageNumber);
|
|
|
|
}
|
2019-07-01 07:56:54 +02:00
|
|
|
} else if (pageNumber === prerenderTrigger + 1) {
|
2019-06-28 05:22:08 +02:00
|
|
|
if (prerenderTrigger - 1 > 0) {
|
|
|
|
renderPage(prerenderTrigger - 1, false, true, prerenderTrigger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:31:16 +02:00
|
|
|
function display(newCanvas, zoom) {
|
2019-06-28 05:22:08 +02:00
|
|
|
canvas.height = newCanvas.height;
|
|
|
|
canvas.width = newCanvas.width;
|
|
|
|
canvas.style.height = newCanvas.style.height;
|
|
|
|
canvas.style.width = newCanvas.style.width;
|
|
|
|
canvas.getContext("2d", { alpha: false }).drawImage(newCanvas, 0, 0);
|
2020-04-04 17:31:16 +02:00
|
|
|
if (!zoom) {
|
|
|
|
scrollTo(0, 0);
|
|
|
|
}
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 09:42:50 +02:00
|
|
|
function setLayerTransform(pageWidth, pageHeight, layerDiv) {
|
|
|
|
const translate = {
|
|
|
|
X: Math.max(0, pageWidth - document.body.clientWidth) / 2,
|
|
|
|
Y: Math.max(0, pageHeight - document.body.clientHeight) / 2
|
|
|
|
};
|
|
|
|
layerDiv.style.translate = `${translate.X}px ${translate.Y}px`;
|
|
|
|
}
|
|
|
|
|
2023-06-09 21:10:57 +02:00
|
|
|
function getDefaultZoomRatio(page, orientationDegrees) {
|
|
|
|
const viewport = page.getViewport({scale: 1, rotation: orientationDegrees});
|
|
|
|
const widthZoomRatio = document.body.clientWidth / viewport.width;
|
|
|
|
const heightZoomRatio = document.body.clientHeight / viewport.height;
|
|
|
|
return Math.max(Math.min(widthZoomRatio, heightZoomRatio, channel.getMaxZoomRatio()), channel.getMinZoomRatio());
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:31:16 +02:00
|
|
|
function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
|
2019-06-28 05:22:08 +02:00
|
|
|
pageRendering = true;
|
|
|
|
useRender = !prerender;
|
|
|
|
|
|
|
|
newPageNumber = pageNumber;
|
2019-11-18 11:49:58 +01:00
|
|
|
newZoomRatio = channel.getZoomRatio();
|
2017-11-14 18:29:10 +01:00
|
|
|
orientationDegrees = channel.getDocumentOrientationDegrees();
|
2019-11-18 11:49:58 +01:00
|
|
|
console.log("page: " + pageNumber + ", zoom: " + newZoomRatio +
|
2017-11-14 18:29:10 +01:00
|
|
|
", orientationDegrees: " + orientationDegrees + ", prerender: " + prerender);
|
2019-06-28 05:22:08 +02:00
|
|
|
for (let i = 0; i < cache.length; i++) {
|
|
|
|
const cached = cache[i];
|
2019-11-18 11:49:58 +01:00
|
|
|
if (cached.pageNumber === pageNumber && cached.zoomRatio === newZoomRatio &&
|
2022-10-03 10:09:07 +02:00
|
|
|
cached.orientationDegrees === orientationDegrees) {
|
2019-06-28 05:22:08 +02:00
|
|
|
if (useRender) {
|
|
|
|
cache.splice(i, 1);
|
|
|
|
cache.push(cached);
|
|
|
|
|
2020-04-04 17:31:16 +02:00
|
|
|
display(cached.canvas, zoom);
|
2019-06-28 05:22:08 +02:00
|
|
|
|
|
|
|
textLayerDiv.replaceWith(cached.textLayerDiv);
|
|
|
|
textLayerDiv = cached.textLayerDiv;
|
2023-04-06 09:42:50 +02:00
|
|
|
setLayerTransform(cached.pageWidth, cached.pageHeight, textLayerDiv);
|
2023-03-17 22:53:36 +01:00
|
|
|
container.style.setProperty("--scale-factor", newZoomRatio.toString());
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pageRendering = false;
|
|
|
|
doPrerender(pageNumber, prerenderTrigger);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pdfDoc.getPage(pageNumber).then(function(page) {
|
|
|
|
if (maybeRenderNextPage()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 21:10:57 +02:00
|
|
|
const defaultZoomRatio = getDefaultZoomRatio(page, orientationDegrees);
|
|
|
|
|
|
|
|
if (cache.length === 0) {
|
|
|
|
zoomRatio = defaultZoomRatio;
|
|
|
|
newZoomRatio = defaultZoomRatio;
|
|
|
|
channel.setZoomRatio(defaultZoomRatio);
|
|
|
|
}
|
|
|
|
|
2023-03-17 23:03:17 +01:00
|
|
|
const viewport = page.getViewport({scale: newZoomRatio, rotation: orientationDegrees});
|
2019-06-28 05:22:08 +02:00
|
|
|
|
|
|
|
if (useRender) {
|
2019-11-18 11:49:58 +01:00
|
|
|
if (newZoomRatio !== zoomRatio) {
|
2019-06-28 05:22:08 +02:00
|
|
|
canvas.style.height = viewport.height + "px";
|
|
|
|
canvas.style.width = viewport.width + "px";
|
|
|
|
}
|
2019-11-18 11:49:58 +01:00
|
|
|
zoomRatio = newZoomRatio;
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
|
2023-03-17 23:03:17 +01:00
|
|
|
if (zoom === 2) {
|
2020-04-04 18:14:38 +02:00
|
|
|
pageRendering = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newCanvas = document.createElement("canvas");
|
2023-08-03 09:58:34 +02:00
|
|
|
const ratio = globalThis.devicePixelRatio;
|
2020-04-04 18:14:38 +02:00
|
|
|
newCanvas.height = viewport.height * ratio;
|
|
|
|
newCanvas.width = viewport.width * ratio;
|
|
|
|
newCanvas.style.height = viewport.height + "px";
|
|
|
|
newCanvas.style.width = viewport.width + "px";
|
|
|
|
const newContext = newCanvas.getContext("2d", { alpha: false });
|
|
|
|
newContext.scale(ratio, ratio);
|
|
|
|
|
2019-06-28 05:22:08 +02:00
|
|
|
task = page.render({
|
|
|
|
canvasContext: newContext,
|
|
|
|
viewport: viewport
|
|
|
|
});
|
|
|
|
|
2019-08-16 07:19:50 +02:00
|
|
|
task.promise.then(function() {
|
2019-06-28 05:22:08 +02:00
|
|
|
task = null;
|
|
|
|
|
|
|
|
let rendered = false;
|
|
|
|
function render() {
|
|
|
|
if (!useRender || rendered) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-04 17:31:16 +02:00
|
|
|
display(newCanvas, zoom);
|
2019-06-28 05:22:08 +02:00
|
|
|
rendered = true;
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
|
2023-03-17 22:51:19 +01:00
|
|
|
const newTextLayerDiv = textLayerDiv.cloneNode();
|
2023-08-03 09:37:01 +02:00
|
|
|
task = renderTextLayer({
|
2023-03-17 22:51:19 +01:00
|
|
|
textContentSource: page.streamTextContent(),
|
|
|
|
container: newTextLayerDiv,
|
2019-06-28 05:22:08 +02:00
|
|
|
viewport: viewport
|
|
|
|
});
|
|
|
|
task.promise.then(function() {
|
|
|
|
task = null;
|
|
|
|
|
|
|
|
render();
|
|
|
|
|
2023-03-18 11:00:25 +01:00
|
|
|
// We use CSS transform to rotate a text layer div of zero
|
|
|
|
// degrees rotation. So, when the rotation is 90 or 270
|
|
|
|
// degrees, set width and height of the text layer div to the
|
|
|
|
// height and width of the canvas, respectively, to prevent
|
|
|
|
// text layer misalignment.
|
|
|
|
if (orientationDegrees % 180 === 0) {
|
|
|
|
newTextLayerDiv.style.height = newCanvas.style.height;
|
|
|
|
newTextLayerDiv.style.width = newCanvas.style.width;
|
|
|
|
} else {
|
|
|
|
newTextLayerDiv.style.height = newCanvas.style.width;
|
|
|
|
newTextLayerDiv.style.width = newCanvas.style.height;
|
|
|
|
}
|
2023-04-06 09:42:50 +02:00
|
|
|
setLayerTransform(viewport.width, viewport.height, newTextLayerDiv);
|
2019-06-28 05:22:08 +02:00
|
|
|
if (useRender) {
|
|
|
|
textLayerDiv.replaceWith(newTextLayerDiv);
|
|
|
|
textLayerDiv = newTextLayerDiv;
|
2023-03-17 22:53:36 +01:00
|
|
|
container.style.setProperty("--scale-factor", newZoomRatio.toString());
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cache.length === maxCached) {
|
2023-06-09 08:10:21 +02:00
|
|
|
cache.shift();
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
cache.push({
|
|
|
|
pageNumber: pageNumber,
|
2019-11-18 11:49:58 +01:00
|
|
|
zoomRatio: newZoomRatio,
|
2017-11-14 18:29:10 +01:00
|
|
|
orientationDegrees: orientationDegrees,
|
2019-06-28 05:22:08 +02:00
|
|
|
canvas: newCanvas,
|
2023-04-06 09:42:50 +02:00
|
|
|
textLayerDiv: newTextLayerDiv,
|
|
|
|
pageWidth: viewport.width,
|
|
|
|
pageHeight: viewport.height
|
2019-06-28 05:22:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
pageRendering = false;
|
|
|
|
doPrerender(pageNumber, prerenderTrigger);
|
|
|
|
}).catch(handleRenderingError);
|
|
|
|
}).catch(handleRenderingError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:37:01 +02:00
|
|
|
globalThis.onRenderPage = function (zoom) {
|
2019-06-28 05:22:08 +02:00
|
|
|
if (pageRendering) {
|
2020-04-04 17:32:04 +02:00
|
|
|
if (newPageNumber === channel.getPage() && newZoomRatio === channel.getZoomRatio() &&
|
2017-11-14 18:29:10 +01:00
|
|
|
orientationDegrees === channel.getDocumentOrientationDegrees()) {
|
2019-06-28 05:22:08 +02:00
|
|
|
useRender = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPending = true;
|
2020-04-04 17:31:16 +02:00
|
|
|
renderPendingZoom = zoom;
|
2019-06-28 05:22:08 +02:00
|
|
|
if (task !== null) {
|
|
|
|
task.cancel();
|
|
|
|
task = null;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-04 17:31:16 +02:00
|
|
|
renderPage(channel.getPage(), zoom, false);
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
2023-08-03 09:37:01 +02:00
|
|
|
};
|
2019-06-28 05:22:08 +02:00
|
|
|
|
2023-08-03 09:37:01 +02:00
|
|
|
globalThis.isTextSelected = function () {
|
2023-08-03 09:58:34 +02:00
|
|
|
return globalThis.getSelection().toString() !== "";
|
2023-08-03 09:37:01 +02:00
|
|
|
};
|
2019-08-17 01:42:51 +02:00
|
|
|
|
2023-08-03 09:37:01 +02:00
|
|
|
globalThis.toggleTextLayerVisibility = function () {
|
2023-03-18 14:36:23 +01:00
|
|
|
let textLayerForeground = "red";
|
|
|
|
let textLayerOpacity = 1;
|
|
|
|
if (isTextLayerVisible) {
|
|
|
|
textLayerForeground = "transparent";
|
|
|
|
textLayerOpacity = 0.2;
|
|
|
|
}
|
|
|
|
document.documentElement.style.setProperty("--text-layer-foreground", textLayerForeground);
|
|
|
|
document.documentElement.style.setProperty("--text-layer-opacity", textLayerOpacity.toString());
|
|
|
|
isTextLayerVisible = !isTextLayerVisible;
|
2023-08-03 09:37:01 +02:00
|
|
|
};
|
2023-03-18 14:36:23 +01:00
|
|
|
|
2023-08-03 09:37:01 +02:00
|
|
|
globalThis.loadDocument = function () {
|
2022-05-01 16:46:40 +02:00
|
|
|
const pdfPassword = channel.getPassword();
|
2023-08-03 09:37:01 +02:00
|
|
|
const loadingTask = getDocument({ url: "https://localhost/placeholder.pdf", password: pdfPassword });
|
2022-05-01 16:46:40 +02:00
|
|
|
loadingTask.onPassword = (_, error) => {
|
2023-08-03 09:37:01 +02:00
|
|
|
if (error === PasswordResponses.NEED_PASSWORD) {
|
2022-05-01 16:46:40 +02:00
|
|
|
channel.showPasswordPrompt();
|
2023-08-03 09:37:01 +02:00
|
|
|
} else if (error === PasswordResponses.INCORRECT_PASSWORD) {
|
2022-05-01 16:46:40 +02:00
|
|
|
channel.invalidPassword();
|
|
|
|
}
|
2023-06-09 08:10:21 +02:00
|
|
|
};
|
2022-05-01 16:46:40 +02:00
|
|
|
|
|
|
|
loadingTask.promise.then(function (newDoc) {
|
2022-05-06 19:07:20 +02:00
|
|
|
channel.onLoaded();
|
2022-05-01 16:46:40 +02:00
|
|
|
pdfDoc = newDoc;
|
|
|
|
channel.setNumPages(pdfDoc.numPages);
|
|
|
|
pdfDoc.getMetadata().then(function (data) {
|
|
|
|
channel.setDocumentProperties(JSON.stringify(data.info));
|
|
|
|
}).catch(function (error) {
|
|
|
|
console.log("getMetadata error: " + error);
|
|
|
|
});
|
|
|
|
renderPage(channel.getPage(), false, false);
|
|
|
|
}, function (reason) {
|
|
|
|
console.error(reason.name + ": " + reason.message);
|
2019-06-28 05:22:08 +02:00
|
|
|
});
|
2023-08-03 09:37:01 +02:00
|
|
|
};
|
2023-04-06 09:42:50 +02:00
|
|
|
|
2023-08-03 09:58:34 +02:00
|
|
|
globalThis.onresize = () => {
|
2023-04-06 09:42:50 +02:00
|
|
|
setLayerTransform(canvas.clientWidth, canvas.clientHeight, textLayerDiv);
|
2023-06-09 08:10:21 +02:00
|
|
|
};
|