2024-11-07 02:15:31 +00:00
|
|
|
{{- if not ($.Page.Store.Get "embed-pdf-count") -}}
|
|
|
|
<link rel="stylesheet" href="/css/pdfjs.css?rnd={{ now.Unix }}">
|
|
|
|
<script type="module" src='/js/pdf.js/build/pdf.mjs'></script>
|
2023-04-21 22:40:43 +00:00
|
|
|
{{- end -}}
|
|
|
|
|
2024-11-07 02:15:31 +00:00
|
|
|
{{- $.Page.Store.Add "embed-pdf-count" 1 -}}
|
2023-04-21 22:40:43 +00:00
|
|
|
<div class="embed-pdf-container" id="embed-pdf-container-{{ substr (.Get "url" | md5) 0 8 }}">
|
2024-11-07 02:15:31 +00:00
|
|
|
<div class="pdf-loadingWrapper" id="pdf-loadingWrapper-{{ substr (.Get "url" | md5) 0 8 }}">
|
|
|
|
<div class="pdf-loading" id="pdf-loading-{{ substr (.Get "url" | md5) 0 8 }}"></div>
|
|
|
|
</div>
|
|
|
|
<div id="overlayText">
|
|
|
|
<a href="{{ .Get "url" }}" aria-label="Download" download>
|
|
|
|
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18">
|
|
|
|
<path d="M9 13c.3 0 .5-.1.7-.3L15.4 7 14 5.6l-4 4V1H8v8.6l-4-4L2.6 7l5.7 5.7c.2.2.4.3.7.3zm-7 2h14v2H2z" />
|
|
|
|
</svg>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<canvas class="pdf-canvas" id="pdf-canvas-{{ substr (.Get "url" | md5) 0 8 }}"></canvas>
|
2023-04-21 22:40:43 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="pdf-paginator" id="pdf-paginator-{{ substr (.Get "url" | md5) 0 8 }}">
|
2024-11-07 02:15:31 +00:00
|
|
|
<button id="pdf-prev-{{ substr (.Get "url" | md5) 0 8 }}">Previous</button>
|
|
|
|
<button id="pdf-next-{{ substr (.Get "url" | md5) 0 8 }}">Next</button>
|
|
|
|
<span>
|
|
|
|
<span class="pdf-pagenum" id="pdf-pagenum-{{ substr (.Get "url" | md5) 0 8 }}"></span> / <span
|
|
|
|
class="pdf-pagecount" id="pdf-pagecount-{{ substr (.Get "url" | md5) 0 8 }}"></span>
|
|
|
|
</span>
|
|
|
|
<a class="pdf-source" id="pdf-source-{{ substr (.Get "url" | md5) 0 8 }}" href="{{ .Get "url" }}">Download the PDF
|
|
|
|
file</a>
|
2023-04-21 22:40:43 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<noscript>
|
2024-11-07 02:15:31 +00:00
|
|
|
<a class="pdf-source" id="pdf-source-noscript-{{ substr (.Get "url" | md5) 0 8 }}" href="{{ .Get "url" }}">Download
|
|
|
|
the PDF file.</a>
|
2023-04-21 22:40:43 +00:00
|
|
|
</noscript>
|
|
|
|
|
2024-11-07 02:15:31 +00:00
|
|
|
<script type="module">
|
|
|
|
(function () {
|
2023-04-21 22:40:43 +00:00
|
|
|
var url = '{{ .Get "url" }}';
|
|
|
|
|
|
|
|
var hidePaginator = "{{ .Get "hidePaginator" }}" === "true";
|
|
|
|
var hideLoader = "{{ .Get "hideLoader" }}" === "true";
|
|
|
|
var selectedPageNum = parseInt("{{ .Get "renderPageNum" }}") || 1;
|
|
|
|
|
|
|
|
// Loaded via <script> tag, create shortcut to access PDF.js exports.
|
2024-11-07 02:15:31 +00:00
|
|
|
var { pdfjsLib } = globalThis;
|
2023-04-21 22:40:43 +00:00
|
|
|
|
|
|
|
// The workerSrc property shall be specified.
|
|
|
|
if (pdfjsLib.GlobalWorkerOptions.workerSrc == '')
|
2024-11-07 02:15:31 +00:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = "{{.Site.BaseURL}}" + 'js/pdf.js/build/pdf.worker.mjs';
|
2023-04-21 22:40:43 +00:00
|
|
|
|
|
|
|
// Change the Scale value for lower or higher resolution.
|
|
|
|
var pdfDoc = null,
|
2024-11-07 02:15:31 +00:00
|
|
|
pageNum = selectedPageNum,
|
|
|
|
pageRendering = false,
|
|
|
|
pageNumPending = null,
|
|
|
|
scale = 3,
|
|
|
|
canvas = document.getElementById('pdf-canvas-{{ substr (.Get "url" | md5) 0 8 }}'),
|
|
|
|
ctx = canvas.getContext('2d'),
|
|
|
|
paginator = document.getElementById("pdf-paginator-{{ substr (.Get "url" | md5) 0 8 }}"),
|
|
|
|
loadingWrapper = document.getElementById('pdf-loadingWrapper-{{ substr (.Get "url" | md5) 0 8 }}');
|
2023-04-21 22:40:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Attempt to show paginator and loader if enabled
|
|
|
|
showPaginator();
|
|
|
|
showLoader();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get page info from document, resize canvas accordingly, and render page.
|
|
|
|
* @param num Page number.
|
|
|
|
*/
|
|
|
|
function renderPage(num) {
|
|
|
|
pageRendering = true;
|
|
|
|
// Using promise to fetch the page
|
2024-11-07 02:15:31 +00:00
|
|
|
pdfDoc.getPage(num).then(function (page) {
|
|
|
|
var viewport = page.getViewport({ scale: scale });
|
2023-04-21 22:40:43 +00:00
|
|
|
canvas.height = viewport.height;
|
|
|
|
canvas.width = viewport.width;
|
|
|
|
|
|
|
|
// Render PDF page into canvas context
|
|
|
|
var renderContext = {
|
|
|
|
canvasContext: ctx,
|
|
|
|
viewport: viewport
|
|
|
|
};
|
|
|
|
var renderTask = page.render(renderContext);
|
|
|
|
|
|
|
|
// Wait for rendering to finish
|
2024-11-07 02:15:31 +00:00
|
|
|
renderTask.promise.then(function () {
|
2023-04-21 22:40:43 +00:00
|
|
|
pageRendering = false;
|
|
|
|
showContent();
|
|
|
|
|
|
|
|
if (pageNumPending !== null) {
|
|
|
|
// New page rendering is pending
|
|
|
|
renderPage(pageNumPending);
|
|
|
|
pageNumPending = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update page counters
|
|
|
|
document.getElementById('pdf-pagenum-{{ substr (.Get "url" | md5) 0 8 }}').textContent = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides loader and shows canvas
|
|
|
|
*/
|
|
|
|
function showContent() {
|
|
|
|
loadingWrapper.style.display = 'none';
|
|
|
|
canvas.style.display = 'block';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we haven't disabled the loader, show loader and hide canvas
|
|
|
|
*/
|
|
|
|
function showLoader() {
|
2024-11-07 02:15:31 +00:00
|
|
|
if (hideLoader) return
|
2023-04-21 22:40:43 +00:00
|
|
|
loadingWrapper.style.display = 'flex';
|
|
|
|
canvas.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we haven't disabled the paginator, show paginator
|
|
|
|
*/
|
|
|
|
function showPaginator() {
|
2024-11-07 02:15:31 +00:00
|
|
|
if (hidePaginator) return
|
2023-04-21 22:40:43 +00:00
|
|
|
paginator.style.display = 'block';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If another page rendering in progress, waits until the rendering is
|
|
|
|
* finished. Otherwise, executes rendering immediately.
|
|
|
|
*/
|
|
|
|
function queueRenderPage(num) {
|
|
|
|
if (pageRendering) {
|
|
|
|
pageNumPending = num;
|
|
|
|
} else {
|
|
|
|
renderPage(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays previous page.
|
|
|
|
*/
|
|
|
|
function onPrevPage() {
|
|
|
|
if (pageNum <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pageNum--;
|
|
|
|
queueRenderPage(pageNum);
|
|
|
|
}
|
|
|
|
document.getElementById('pdf-prev-{{ substr (.Get "url" | md5) 0 8 }}').addEventListener('click', onPrevPage);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays next page.
|
|
|
|
*/
|
|
|
|
function onNextPage() {
|
|
|
|
if (pageNum >= pdfDoc.numPages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pageNum++;
|
|
|
|
queueRenderPage(pageNum);
|
|
|
|
}
|
|
|
|
document.getElementById('pdf-next-{{ substr (.Get "url" | md5) 0 8 }}').addEventListener('click', onNextPage);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously downloads PDF.
|
|
|
|
*/
|
2024-11-07 02:15:31 +00:00
|
|
|
pdfjsLib.getDocument(url).promise.then(function (pdfDoc_) {
|
2023-04-21 22:40:43 +00:00
|
|
|
pdfDoc = pdfDoc_;
|
|
|
|
var numPages = pdfDoc.numPages;
|
|
|
|
document.getElementById('pdf-pagecount-{{ substr (.Get "url" | md5) 0 8 }}').textContent = numPages;
|
|
|
|
|
|
|
|
// If the user passed in a number that is out of range, render the last page.
|
2024-11-07 02:15:31 +00:00
|
|
|
if (pageNum > numPages) {
|
2023-04-21 22:40:43 +00:00
|
|
|
pageNum = numPages
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial/first page rendering
|
|
|
|
renderPage(pageNum);
|
|
|
|
});
|
2024-11-07 02:15:31 +00:00
|
|
|
})();
|
|
|
|
</script>
|