Welcome to mirror list, hosted at ThFree Co, Russian Federation.

embed-pdf.html « shortcodes « layouts - github.com/hossainemruz/toha.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8a446958e6c9c165c98623106c83ac8ffef7c2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!--

SPDX-License-Identifier: MIT

This shortcut was originally taken from: https://github.com/anvithks/hugo-embed-pdf-shortcode,
where it is available under the MIT License, where it was originally created by https://github.com/anvithks

Since the project seems discontinued, it has been re-created here
-->

<!-- Load the PDF-JS from the local folder (should be updated over time) -->
<script src= '/js/pdf-js/build/pdf.js'></script>

<!-- Set the navigation menu -->
<div id="paginator">
    <button id="prev">Previous</button>
    <button id="next">Next</button>
    &nbsp; &nbsp;
    <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>

<!-- And the canvas where the PDF will load -->
<div id="embed-pdf-container">
    <div id="loadingWrapper">
      <div id="loading"></div>
    </div>
    <canvas id="the-canvas"></canvas>
</div>

<!-- This script gets the PDF, sets it and passes it on to the already-loaded pdf-js -->
<script type="text/javascript">
window.onload = function() {
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = "{{.Site.BaseURL}}" + '{{ .Get "src" }}';

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.
var pdfjsLib = window['pdfjs-dist/build/pdf'];

// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = "{{.Site.BaseURL}}" + '/js/pdf-js/build/pdf.worker.js';

// Change the Scale value for lower or higher resolution.
var pdfDoc = null,
    pageNum = selectedPageNum,
    pageRendering = false,
    pageNumPending = null,
    scale = 3,
    canvas = document.getElementById('the-canvas'),
    ctx = canvas.getContext('2d'),
    paginator = document.getElementById("paginator"),
    loadingWrapper = document.getElementById('loadingWrapper');


// 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
  pdfDoc.getPage(num).then(function(page) {
    var viewport = page.getViewport({scale: scale});
    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
    renderTask.promise.then(function() {
      pageRendering = false;
      showContent();

      if (pageNumPending !== null) {
        // New page rendering is pending
        renderPage(pageNumPending);
        pageNumPending = null;
      }
    });
  });

  // Update page counters
  document.getElementById('page_num').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() {
  if(hideLoader) return
  loadingWrapper.style.display = 'flex';
  canvas.style.display = 'none';
}

/**
 * If we haven't disabled the paginator, show paginator
 */
function showPaginator() {
  if(hidePaginator) return
  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('prev').addEventListener('click', onPrevPage);

/**
 * Displays next page.
 */
function onNextPage() {
  if (pageNum >= pdfDoc.numPages) {
    return;
  }
  pageNum++;
  queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);

/**
 * Asynchronously downloads PDF.
 */
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
  pdfDoc = pdfDoc_;
  var numPages = pdfDoc.numPages;
  document.getElementById('page_count').textContent = numPages;

  // If the user passed in a number that is out of range, render the last page.
  if(pageNum > numPages) {
    pageNum = numPages
  }

  // Initial/first page rendering
  renderPage(pageNum);
});
}

</script>

<!-- Finally, make the canvas more beautiful -->
<style>
#the-canvas {
  border: 1px solid black;
  direction: ltr;
  width: 100%;
  height: auto;
  display: none;
}

#paginator {
  display: none;
  text-align: center;
  margin-bottom: 10px;
}

#loadingWrapper {
  display: none;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 350px;
}

#loading {
  display: inline-block;
  width: 50px;
  height: 50px;
  border: 3px solid #d2d0d0;;
  border-radius: 50%;
  border-top-color: #383838;
  animation: spin 1s ease-in-out infinite;
  -webkit-animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to { -webkit-transform: rotate(360deg); }
}
@-webkit-keyframes spin {
  to { -webkit-transform: rotate(360deg); }
}
</style>