comparison Applications/StoneWebViewer/WebApplication/pdf-viewer.js @ 1657:66e5fcdf5597

pdf viewer is working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Nov 2020 11:19:09 +0100
parents fa9e6bf84958
children e4589378ad8b
comparison
equal deleted inserted replaced
1656:4cdc297be5a6 1657:66e5fcdf5597
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 21
22 22
23 /**
24 * This source file is an adaptation for Vue.js of the sample code
25 * "Previous/Next example" of PDF.js:
26 * https://mozilla.github.io/pdf.js/examples/
27 *
28 * =======================================================================
29 *
30 * Original license of the sample code:
31 *
32 * Copyright 2014 Mozilla Foundation
33 *
34 * Licensed under the Apache License, Version 2.0 (the "License");
35 * you may not use this file except in compliance with the License.
36 * You may obtain a copy of the License at
37 *
38 * http://www.apache.org/licenses/LICENSE-2.0
39 *
40 * Unless required by applicable law or agreed to in writing, software
41 * distributed under the License is distributed on an "AS IS" BASIS,
42 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 * See the License for the specific language governing permissions and
44 * limitations under the License.
45 **/
46
23 47
24 // Loaded via <script> tag, create shortcut to access PDF.js exports. 48 // Loaded via <script> tag, create shortcut to access PDF.js exports.
25 var pdfjsLib = window['pdfjs-dist/build/pdf']; 49 var pdfjsLib = window['pdfjs-dist/build/pdf'];
26 50
27 // The workerSrc property shall be specified. 51 // The workerSrc property shall be specified.
28 pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/build/pdf.worker.min.js'; 52 pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/build/pdf.worker.min.js';
29 53
30 54
31 55
32 var ZOOM_FACTOR = 1.3; 56 var ZOOM_FACTOR = 1.3;
33 var FIT_MARGIN = 10; 57 var FIT_MARGIN = 10; // Additional margin for width/height fitting, in order to avoid spurious scrollbars
34 58
35 Vue.component('pdf-viewer', { 59 Vue.component('pdf-viewer', {
36 props: [ 'prefix', 'pdf' ], // "pdf" must correspond to a "Uint8Array" 60 props: [ 'prefix' ],
37 template: '#pdf-viewer', 61 template: '#pdf-viewer',
38 data: function() { 62 data: function() {
39 return { 63 return {
40 container: null, 64 container: null,
41 canvas: null, 65 canvas: null,
42 ctx: null, 66 ctx: null,
67 pdf: null, // "pdf" must correspond to a "Uint8Array"
43 68
44 scale: 1, 69 scale: 1,
45 countPages: 0, 70 countPages: 0,
46 currentPage: 0, 71 currentPage: 0,
47 pdfDoc: null, 72 pdfDoc: null,
48 isRendering: false, 73 isRendering: false,
49 pageNumPending: null 74 pageNumPending: null
50 }
51 },
52 watch: {
53 pdf: function(newVal, oldVal) {
54 this.LoadPdf();
55 } 75 }
56 }, 76 },
57 mounted: function() { 77 mounted: function() {
58 this.container = document.getElementById(this.prefix + '-container'); 78 this.container = document.getElementById(this.prefix + '-container');
59 this.canvas = document.getElementById(this.prefix + '-canvas'); 79 this.canvas = document.getElementById(this.prefix + '-canvas');
104 // on the parent element of the canvas. 124 // on the parent element of the canvas.
105 125
106 // https://github.com/mozilla/pdf.js/issues/5628 126 // https://github.com/mozilla/pdf.js/issues/5628
107 var scrollbarHeight = window.innerHeight - document.body.clientHeight + FIT_MARGIN; 127 var scrollbarHeight = window.innerHeight - document.body.clientHeight + FIT_MARGIN;
108 that.scale = (that.container.offsetHeight - scrollbarHeight) / page.getViewport({ scale: 1.0 }).height; 128 that.scale = (that.container.offsetHeight - scrollbarHeight) / page.getViewport({ scale: 1.0 }).height;
109 //that.scale = that.container.clientHeight / page.getViewport({ scale: 1.0 }).height;
110 that.QueueRenderPage(that.currentPage); 129 that.QueueRenderPage(that.currentPage);
111 }); 130 });
112 } 131 }
113 }, 132 },
114 ZoomIn: function() { 133 ZoomIn: function() {
118 ZoomOut: function() { 137 ZoomOut: function() {
119 this.scale /= ZOOM_FACTOR; 138 this.scale /= ZOOM_FACTOR;
120 this.QueueRenderPage(this.currentPage); 139 this.QueueRenderPage(this.currentPage);
121 }, 140 },
122 LoadPdf: function(pdf) { 141 LoadPdf: function(pdf) {
123 var that = this; 142 if (!this.isRendering &&
124 pdfjsLib.getDocument(new Uint8Array(this.pdf)).promise.then(function(pdfDoc_) { 143 pdf.length > 0) {
125 that.pdfDoc = pdfDoc_; 144 this.pdf = pdf;
126 that.currentPage = 0; 145 this.isRendering = true;
127 that.countPages = pdfDoc_.numPages; 146
128 that.scale = 1; 147 var that = this;
129 that.isRendering = false; 148 pdfjsLib.getDocument(this.pdf).promise.then(function(pdfDoc_) {
130 that.pageNumPending = null; 149 that.pdfDoc = pdfDoc_;
131 150 that.currentPage = 1;
132 // Initial/first page rendering 151 that.countPages = pdfDoc_.numPages;
133 that.RenderPage(1); 152 that.scale = 1;
134 }); 153 that.isRendering = false;
154 that.pageNumPending = null;
155
156 // Initial/first page rendering, after fitting the PDF to the available viewport
157 that.FitHeight();
158 });
159 }
135 }, 160 },
136 RenderPage: function(pageNum) { 161 RenderPage: function(pageNum) {
137 var that = this; 162 var that = this;
138 163
139 if (this.pdfDoc !== null && 164 if (this.pdfDoc !== null &&
185 }, 210 },
186 MouseWheel: function(event) { 211 MouseWheel: function(event) {
187 if (event.ctrlKey) { 212 if (event.ctrlKey) {
188 if (event.deltaY < 0) { 213 if (event.deltaY < 0) {
189 this.ZoomIn(); 214 this.ZoomIn();
215 event.preventDefault();
190 } else if (event.deltaY > 0) { 216 } else if (event.deltaY > 0) {
191 this.ZoomOut(); 217 this.ZoomOut();
218 event.preventDefault();
192 } 219 }
193 220 } else if (!event.shiftKey &&
194 event.preventDefault(); 221 !event.altKey &&
222 !event.metaKey) {
223 // Is the vertical scrollbar hidden?
224 // https://stackoverflow.com/a/4814526/881731
225 if (this.container.scrollHeight <= this.container.clientHeight) {
226 if (event.deltaY < 0) {
227 this.PreviousPage();
228 event.preventDefault();
229 } else if (event.deltaY > 0) {
230 this.NextPage();
231 event.preventDefault();
232 }
233 }
195 } 234 }
196 } 235 }
197 } 236 }
198 }); 237 });