annotate Applications/StoneWebViewer/WebApplication/pdf-viewer.js @ 2088:b98d159c7545 dicom-sr

created branch dicom-sr
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Nov 2023 18:12:35 +0100
parents 07964689cb0b
children c23eef785569
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
2077
07964689cb0b upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
07964689cb0b upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU Affero General Public License
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Affero General Public License for more details.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU Affero General Public License
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
24 /**
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
25 * This source file is an adaptation for Vue.js of the sample code
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
26 * "Previous/Next example" of PDF.js:
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
27 * https://mozilla.github.io/pdf.js/examples/
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
28 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
29 * =======================================================================
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
30 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
31 * Original license of the sample code:
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
32 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
33 * Copyright 2014 Mozilla Foundation
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
34 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
35 * Licensed under the Apache License, Version 2.0 (the "License");
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
36 * you may not use this file except in compliance with the License.
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
37 * You may obtain a copy of the License at
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
38 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
39 * http://www.apache.org/licenses/LICENSE-2.0
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
40 *
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
41 * Unless required by applicable law or agreed to in writing, software
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
42 * distributed under the License is distributed on an "AS IS" BASIS,
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
44 * See the License for the specific language governing permissions and
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
45 * limitations under the License.
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
46 **/
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
47
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 // Loaded via <script> tag, create shortcut to access PDF.js exports.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 var pdfjsLib = window['pdfjs-dist/build/pdf'];
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 // The workerSrc property shall be specified.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.5.207/build/pdf.worker.min.js';
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 var ZOOM_FACTOR = 1.3;
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
58 var FIT_MARGIN = 10; // Additional margin for width/height fitting, in order to avoid spurious scrollbars
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 Vue.component('pdf-viewer', {
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
61 props: [ 'prefix' ],
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 template: '#pdf-viewer',
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 data: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 return {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 container: null,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 canvas: null,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 ctx: null,
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
68 pdf: null, // "pdf" must correspond to a "Uint8Array"
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 scale: 1,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 countPages: 0,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 currentPage: 0,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 pdfDoc: null,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 isRendering: false,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 pageNumPending: null
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 mounted: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 this.container = document.getElementById(this.prefix + '-container');
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 this.canvas = document.getElementById(this.prefix + '-canvas');
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 this.ctx = this.canvas.getContext('2d');
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 if (this.container === null ||
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 this.canvas === null ||
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 this.ctx === null) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 alert('Bad viewer configuration');
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 var that = this;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 this.container.addEventListener('wheel', function(event) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 that.MouseWheel(event);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 });
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 methods: {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 NextPage: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 if (this.pdfDoc !== null &&
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 this.currentPage < this.pdfDoc.numPages) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 this.QueueRenderPage(this.currentPage + 1);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 PreviousPage: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 if (this.pdfDoc !== null &&
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 this.currentPage > 1) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 this.QueueRenderPage(this.currentPage - 1);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 FitWidth: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if (this.pdfDoc !== null) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 var that = this;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 this.pdfDoc.getPage(this.currentPage).then(function(page) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 // https://github.com/mozilla/pdf.js/issues/5628
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 // https://stackoverflow.com/a/21064102/881731
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 // https://stackoverflow.com/a/60008044/881731
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 var scrollbarWidth = window.innerWidth - document.body.clientWidth + FIT_MARGIN;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 that.scale = (that.container.offsetWidth - scrollbarWidth) / page.getViewport({ scale: 1.0 }).width;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 that.QueueRenderPage(that.currentPage);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 });
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 FitHeight: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 if (this.pdfDoc !== null) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 var that = this;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 this.pdfDoc.getPage(this.currentPage).then(function(page) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 // The computation below assumes that "line-height: 0px" CSS
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 // on the parent element of the canvas.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 // https://github.com/mozilla/pdf.js/issues/5628
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 var scrollbarHeight = window.innerHeight - document.body.clientHeight + FIT_MARGIN;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 that.scale = (that.container.offsetHeight - scrollbarHeight) / page.getViewport({ scale: 1.0 }).height;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 that.QueueRenderPage(that.currentPage);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 });
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 ZoomIn: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 this.scale *= ZOOM_FACTOR;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 this.QueueRenderPage(this.currentPage);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 ZoomOut: function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 this.scale /= ZOOM_FACTOR;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 this.QueueRenderPage(this.currentPage);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 LoadPdf: function(pdf) {
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
143 if (!this.isRendering &&
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
144 pdf.length > 0) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
145 this.pdf = pdf;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
146 this.isRendering = true;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
147
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
148 var that = this;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
149 pdfjsLib.getDocument(this.pdf).promise.then(function(pdfDoc_) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
150 that.pdfDoc = pdfDoc_;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
151 that.currentPage = 1;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
152 that.countPages = pdfDoc_.numPages;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
153 that.scale = 1;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
154 that.isRendering = false;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
155 that.pageNumPending = null;
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
156
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
157 // Initial/first page rendering, after fitting the PDF to the available viewport
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
158 that.FitHeight();
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
159 });
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
160 }
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 RenderPage: function(pageNum) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 var that = this;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 if (this.pdfDoc !== null &&
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 pageNum >= 1 &&
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 pageNum <= this.countPages) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 this.isRendering = true;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 this.pdfDoc.getPage(pageNum).then(function(page) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 var viewport = page.getViewport({scale: that.scale});
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
1661
e4589378ad8b warning message about unsupported videos
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1657
diff changeset
172 that.canvas.style['background-color'] = 'white'; // avoids flickering while the page changes
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 that.canvas.height = viewport.height;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 that.canvas.width = viewport.width;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 // Horizontal centering of the canvas. This requires CSS
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 // "position: relative" on the canvas element.
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 if (that.canvas.width < that.container.clientWidth) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 that.canvas.style.left = Math.floor((that.container.clientWidth - viewport.width) / 2) + 'px';
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 } else {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 that.canvas.style.left = '0px';
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 // Render PDF page into canvas context
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 var renderContext = {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 canvasContext: that.ctx,
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 viewport: viewport
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 };
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 var renderTask = page.render(renderContext);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 // Wait for rendering to finish
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 renderTask.promise.then(function() {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 that.isRendering = false;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 that.currentPage = pageNum;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 if (that.pageNumPending !== null) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 // New page rendering is pending
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 that.currentPage = that.pageNumPending;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 that.pageNumPending = null;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 that.RenderPage();
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 });
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 });
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 QueueRenderPage: function(pageNum) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 if (this.isRendering) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 this.pageNumPending = pageNum;
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 } else {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 this.RenderPage(pageNum);
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 },
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 MouseWheel: function(event) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 if (event.ctrlKey) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 if (event.deltaY < 0) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 this.ZoomIn();
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
217 event.preventDefault();
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 } else if (event.deltaY > 0) {
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 this.ZoomOut();
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
220 event.preventDefault();
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
1657
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
222 } else if (!event.shiftKey &&
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
223 !event.altKey &&
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
224 !event.metaKey) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
225 // Is the vertical scrollbar hidden?
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
226 // https://stackoverflow.com/a/4814526/881731
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
227 if (this.container.scrollHeight <= this.container.clientHeight) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
228 if (event.deltaY < 0) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
229 this.PreviousPage();
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
230 event.preventDefault();
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
231 } else if (event.deltaY > 0) {
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
232 this.NextPage();
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
233 event.preventDefault();
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
234 }
66e5fcdf5597 pdf viewer is working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1652
diff changeset
235 }
1652
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
fa9e6bf84958 integrating pdf.js into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 });