@progress/kendo-pdfviewer-common 0.2.2 → 0.2.3-dev.202310060852
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/utils.js +116 -31
- package/dist/es2015/utils.js +116 -31
- package/dist/npm/utils.d.ts +19 -1
- package/dist/npm/utils.js +116 -31
- package/package.json +3 -2
package/dist/es/utils.js
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { saveAs } from '@progress/kendo-file-saver';
|
|
2
|
+
import { detectDesktopBrowser, detectMobileOS } from "@progress/kendo-common";
|
|
2
3
|
import { getDocument, renderTextLayer } from 'pdfjs-dist';
|
|
4
|
+
const MAX_CANVAS_WIDTH_HEIGHT_CHROME = 65535;
|
|
5
|
+
const MAX_CANVAS_AREA_CHROME_SAFARI = 268435456;
|
|
6
|
+
const MAX_CANVAS_WIDTH_HEIGHT_FIREFOX = 32767;
|
|
7
|
+
const MAX_CANVAS_AREA_FIREFOX = 124992400;
|
|
8
|
+
const MAX_CANVAS_WIDTH_SAFARI = 4194303;
|
|
9
|
+
const MAX_CANVAS_HEIGHT_SAFARI = 8388607;
|
|
3
10
|
const isIOS = () => /iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.maxTouchPoints &&
|
|
4
11
|
navigator.maxTouchPoints > 2 &&
|
|
5
12
|
/Macintosh/i.test(navigator.userAgent));
|
|
13
|
+
const isSafari = (userAgent) => {
|
|
14
|
+
return detectDesktopBrowser(userAgent).safari ||
|
|
15
|
+
(detectMobileOS(userAgent) && detectMobileOS(userAgent).browser === 'mobilesafari');
|
|
16
|
+
};
|
|
17
|
+
const isFirefox = (userAgent) => {
|
|
18
|
+
const desktopBrowser = detectDesktopBrowser(userAgent);
|
|
19
|
+
const mobileOS = detectMobileOS(userAgent);
|
|
20
|
+
return (desktopBrowser && desktopBrowser.mozilla) || (mobileOS && mobileOS.browser === 'firefox');
|
|
21
|
+
};
|
|
6
22
|
/**
|
|
7
23
|
* @hidden
|
|
8
24
|
*/
|
|
@@ -152,21 +168,7 @@ const openPrintDialog = (dom, width, height, done, onError) => {
|
|
|
152
168
|
}
|
|
153
169
|
};
|
|
154
170
|
const renderCanvas = (page, done, error) => {
|
|
155
|
-
const scaleNum =
|
|
156
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
157
|
-
const styles = {
|
|
158
|
-
width: Math.floor(viewport.width / scaleNum) + 'pt',
|
|
159
|
-
height: Math.floor(viewport.height / scaleNum) + 'pt'
|
|
160
|
-
};
|
|
161
|
-
const pageElement = createElement('div', '', styles);
|
|
162
|
-
const canvas = createElement('canvas', '', {
|
|
163
|
-
width: '100%',
|
|
164
|
-
height: '100%'
|
|
165
|
-
});
|
|
166
|
-
canvas.height = viewport.height;
|
|
167
|
-
canvas.width = viewport.width;
|
|
168
|
-
const canvasContext = canvas.getContext('2d');
|
|
169
|
-
pageElement.appendChild(canvas);
|
|
171
|
+
const { canvasContext, viewport, scaleNum, canvas, pageElement } = createCanvas(page);
|
|
170
172
|
page.render({ canvasContext, viewport })
|
|
171
173
|
.promise.then(() => {
|
|
172
174
|
const printContent = new Image(Math.floor((viewport.width / scaleNum)), Math.floor((viewport.height / scaleNum)));
|
|
@@ -191,21 +193,7 @@ const createElement = function (name, className, styles) {
|
|
|
191
193
|
return element;
|
|
192
194
|
};
|
|
193
195
|
const renderPage = (page, zoom, error) => {
|
|
194
|
-
const
|
|
195
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
196
|
-
const styles = {
|
|
197
|
-
width: (Math.floor(viewport.width / scaleNum) * zoom) + 'pt',
|
|
198
|
-
height: (Math.floor(viewport.height / scaleNum) * zoom) + 'pt'
|
|
199
|
-
};
|
|
200
|
-
const pageElement = createElement('div', 'k-page', styles);
|
|
201
|
-
const canvas = createElement('canvas', '', {
|
|
202
|
-
width: '100%',
|
|
203
|
-
height: '100%'
|
|
204
|
-
});
|
|
205
|
-
canvas.height = viewport.height;
|
|
206
|
-
canvas.width = viewport.width;
|
|
207
|
-
const canvasContext = canvas.getContext('2d');
|
|
208
|
-
pageElement.appendChild(canvas);
|
|
196
|
+
const { canvasContext, viewport, pageElement, styles } = createCanvas(page, zoom, 'k-page');
|
|
209
197
|
page.render({ canvasContext, viewport })
|
|
210
198
|
.promise.then(() => {
|
|
211
199
|
page.getTextContent().then((textContent) => {
|
|
@@ -298,9 +286,27 @@ export const calculateZoomLevel = (zoomLevel, zoomLevelType, currentZoom, dom) =
|
|
|
298
286
|
};
|
|
299
287
|
/**
|
|
300
288
|
* Scrolls the PDFViewer document to the passed page number.
|
|
301
|
-
*
|
|
302
289
|
* @param rootElement The root HTML element of the PDFViewer component.
|
|
303
290
|
* @param pageNumber The page number.
|
|
291
|
+
* @example
|
|
292
|
+
* ```jsx
|
|
293
|
+
* function App() {
|
|
294
|
+
* const pdfRef = React.useRef(null);
|
|
295
|
+
* const handleClick = () => {
|
|
296
|
+
* scrollToPage(pdfRef.current.element, 3);
|
|
297
|
+
* };
|
|
298
|
+
* return (
|
|
299
|
+
* <div>
|
|
300
|
+
* <Button onClick={handleClick} >
|
|
301
|
+
* Scroll to Page 3
|
|
302
|
+
* </Button>
|
|
303
|
+
* <PDFViewer
|
|
304
|
+
* ref={pdfRef}
|
|
305
|
+
* />
|
|
306
|
+
* </div>
|
|
307
|
+
* )
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
304
310
|
*/
|
|
305
311
|
export const scrollToPage = (rootElement, pageNumber) => {
|
|
306
312
|
const pages = rootElement.querySelectorAll('.k-page');
|
|
@@ -329,3 +335,82 @@ export const currentPage = (rootElement) => {
|
|
|
329
335
|
0.01)
|
|
330
336
|
: 0;
|
|
331
337
|
};
|
|
338
|
+
/**
|
|
339
|
+
* @hidden
|
|
340
|
+
*
|
|
341
|
+
* related to https://github.com/telerik/kendo-pdfviewer-common/issues/6
|
|
342
|
+
* the bigger the canvas size, the worse the performance;
|
|
343
|
+
* if initial size after scaling is greater than browser limits,
|
|
344
|
+
* we are limiting it to the limits, then halving it for performance.
|
|
345
|
+
*/
|
|
346
|
+
const adjustCanvasSize = (targetWidth, targetHeight) => {
|
|
347
|
+
const { maxWidth, maxHeight, maxArea } = isFirefox(navigator.userAgent) ?
|
|
348
|
+
{ maxWidth: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxHeight: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxArea: MAX_CANVAS_AREA_FIREFOX } :
|
|
349
|
+
isSafari(navigator.userAgent) ?
|
|
350
|
+
{ maxWidth: MAX_CANVAS_WIDTH_SAFARI, maxHeight: MAX_CANVAS_HEIGHT_SAFARI, maxArea: MAX_CANVAS_AREA_CHROME_SAFARI } :
|
|
351
|
+
{
|
|
352
|
+
maxWidth: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
353
|
+
maxHeight: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
354
|
+
maxArea: MAX_CANVAS_AREA_CHROME_SAFARI
|
|
355
|
+
};
|
|
356
|
+
let adjustedWidth = targetWidth;
|
|
357
|
+
let adjustedHeight = targetHeight;
|
|
358
|
+
const ratio = targetWidth / targetHeight;
|
|
359
|
+
if (targetWidth > maxWidth) {
|
|
360
|
+
adjustedWidth = maxWidth;
|
|
361
|
+
const deltaWidth = targetWidth - maxWidth;
|
|
362
|
+
const deltaHeight = deltaWidth / ratio;
|
|
363
|
+
adjustedHeight = targetHeight - deltaHeight;
|
|
364
|
+
}
|
|
365
|
+
if (adjustedHeight > maxHeight) {
|
|
366
|
+
const deltaHeight = adjustedHeight - maxHeight;
|
|
367
|
+
const deltaWidth = deltaHeight * ratio;
|
|
368
|
+
adjustedHeight = maxHeight;
|
|
369
|
+
adjustedWidth -= deltaWidth;
|
|
370
|
+
}
|
|
371
|
+
const adjustedArea = adjustedWidth * adjustedHeight;
|
|
372
|
+
if (adjustedArea > maxArea) {
|
|
373
|
+
const areaRatio = Math.sqrt(maxArea / adjustedArea);
|
|
374
|
+
adjustedWidth *= areaRatio;
|
|
375
|
+
adjustedHeight *= areaRatio;
|
|
376
|
+
}
|
|
377
|
+
const adjustRatio = adjustedWidth / targetWidth;
|
|
378
|
+
return {
|
|
379
|
+
adjustedWidth: adjustedWidth !== targetWidth ? Math.floor(adjustedWidth / 2) : targetWidth,
|
|
380
|
+
adjustedHeight: adjustedHeight !== targetHeight ? Math.floor(adjustedHeight / 2) : targetHeight,
|
|
381
|
+
adjustRatio: adjustRatio !== 1 ? adjustRatio / 2 : 1
|
|
382
|
+
};
|
|
383
|
+
};
|
|
384
|
+
const createCanvas = (page, zoom = 1, cssClass = '') => {
|
|
385
|
+
const scaleNum = scale();
|
|
386
|
+
const viewport = page.getViewport({ scale: scaleNum });
|
|
387
|
+
const { adjustedWidth, adjustedHeight, adjustRatio } = adjustCanvasSize(viewport.width, viewport.height);
|
|
388
|
+
const styles = {
|
|
389
|
+
width: Math.floor(viewport.width / scaleNum) * zoom + 'pt',
|
|
390
|
+
height: Math.floor(viewport.height / scaleNum) * zoom + 'pt'
|
|
391
|
+
};
|
|
392
|
+
const pageElement = createElement('div', cssClass, styles);
|
|
393
|
+
const canvas = createElement('canvas', '', {
|
|
394
|
+
width: '100%',
|
|
395
|
+
height: '100%'
|
|
396
|
+
});
|
|
397
|
+
canvas.height = adjustedHeight;
|
|
398
|
+
canvas.width = adjustedWidth;
|
|
399
|
+
const canvasContext = canvas.getContext('2d');
|
|
400
|
+
pageElement.appendChild(canvas);
|
|
401
|
+
const adjustedScale = adjustRatio * scaleNum;
|
|
402
|
+
viewport.width = adjustedWidth;
|
|
403
|
+
viewport.height = adjustedHeight;
|
|
404
|
+
viewport.scale = adjustedScale;
|
|
405
|
+
viewport.transform[0] = adjustedScale;
|
|
406
|
+
viewport.transform[3] = -adjustedScale;
|
|
407
|
+
viewport.transform[5] = adjustedHeight;
|
|
408
|
+
return {
|
|
409
|
+
canvasContext,
|
|
410
|
+
viewport,
|
|
411
|
+
scaleNum,
|
|
412
|
+
canvas,
|
|
413
|
+
pageElement,
|
|
414
|
+
styles
|
|
415
|
+
};
|
|
416
|
+
};
|
package/dist/es2015/utils.js
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { saveAs } from '@progress/kendo-file-saver';
|
|
2
|
+
import { detectDesktopBrowser, detectMobileOS } from "@progress/kendo-common";
|
|
2
3
|
import { getDocument, renderTextLayer } from 'pdfjs-dist';
|
|
4
|
+
const MAX_CANVAS_WIDTH_HEIGHT_CHROME = 65535;
|
|
5
|
+
const MAX_CANVAS_AREA_CHROME_SAFARI = 268435456;
|
|
6
|
+
const MAX_CANVAS_WIDTH_HEIGHT_FIREFOX = 32767;
|
|
7
|
+
const MAX_CANVAS_AREA_FIREFOX = 124992400;
|
|
8
|
+
const MAX_CANVAS_WIDTH_SAFARI = 4194303;
|
|
9
|
+
const MAX_CANVAS_HEIGHT_SAFARI = 8388607;
|
|
3
10
|
const isIOS = () => /iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.maxTouchPoints &&
|
|
4
11
|
navigator.maxTouchPoints > 2 &&
|
|
5
12
|
/Macintosh/i.test(navigator.userAgent));
|
|
13
|
+
const isSafari = (userAgent) => {
|
|
14
|
+
return detectDesktopBrowser(userAgent).safari ||
|
|
15
|
+
(detectMobileOS(userAgent) && detectMobileOS(userAgent).browser === 'mobilesafari');
|
|
16
|
+
};
|
|
17
|
+
const isFirefox = (userAgent) => {
|
|
18
|
+
const desktopBrowser = detectDesktopBrowser(userAgent);
|
|
19
|
+
const mobileOS = detectMobileOS(userAgent);
|
|
20
|
+
return (desktopBrowser && desktopBrowser.mozilla) || (mobileOS && mobileOS.browser === 'firefox');
|
|
21
|
+
};
|
|
6
22
|
/**
|
|
7
23
|
* @hidden
|
|
8
24
|
*/
|
|
@@ -152,21 +168,7 @@ const openPrintDialog = (dom, width, height, done, onError) => {
|
|
|
152
168
|
}
|
|
153
169
|
};
|
|
154
170
|
const renderCanvas = (page, done, error) => {
|
|
155
|
-
const scaleNum =
|
|
156
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
157
|
-
const styles = {
|
|
158
|
-
width: Math.floor(viewport.width / scaleNum) + 'pt',
|
|
159
|
-
height: Math.floor(viewport.height / scaleNum) + 'pt'
|
|
160
|
-
};
|
|
161
|
-
const pageElement = createElement('div', '', styles);
|
|
162
|
-
const canvas = createElement('canvas', '', {
|
|
163
|
-
width: '100%',
|
|
164
|
-
height: '100%'
|
|
165
|
-
});
|
|
166
|
-
canvas.height = viewport.height;
|
|
167
|
-
canvas.width = viewport.width;
|
|
168
|
-
const canvasContext = canvas.getContext('2d');
|
|
169
|
-
pageElement.appendChild(canvas);
|
|
171
|
+
const { canvasContext, viewport, scaleNum, canvas, pageElement } = createCanvas(page);
|
|
170
172
|
page.render({ canvasContext, viewport })
|
|
171
173
|
.promise.then(() => {
|
|
172
174
|
const printContent = new Image(Math.floor((viewport.width / scaleNum)), Math.floor((viewport.height / scaleNum)));
|
|
@@ -191,21 +193,7 @@ const createElement = function (name, className, styles) {
|
|
|
191
193
|
return element;
|
|
192
194
|
};
|
|
193
195
|
const renderPage = (page, zoom, error) => {
|
|
194
|
-
const
|
|
195
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
196
|
-
const styles = {
|
|
197
|
-
width: (Math.floor(viewport.width / scaleNum) * zoom) + 'pt',
|
|
198
|
-
height: (Math.floor(viewport.height / scaleNum) * zoom) + 'pt'
|
|
199
|
-
};
|
|
200
|
-
const pageElement = createElement('div', 'k-page', styles);
|
|
201
|
-
const canvas = createElement('canvas', '', {
|
|
202
|
-
width: '100%',
|
|
203
|
-
height: '100%'
|
|
204
|
-
});
|
|
205
|
-
canvas.height = viewport.height;
|
|
206
|
-
canvas.width = viewport.width;
|
|
207
|
-
const canvasContext = canvas.getContext('2d');
|
|
208
|
-
pageElement.appendChild(canvas);
|
|
196
|
+
const { canvasContext, viewport, pageElement, styles } = createCanvas(page, zoom, 'k-page');
|
|
209
197
|
page.render({ canvasContext, viewport })
|
|
210
198
|
.promise.then(() => {
|
|
211
199
|
page.getTextContent().then((textContent) => {
|
|
@@ -298,9 +286,27 @@ export const calculateZoomLevel = (zoomLevel, zoomLevelType, currentZoom, dom) =
|
|
|
298
286
|
};
|
|
299
287
|
/**
|
|
300
288
|
* Scrolls the PDFViewer document to the passed page number.
|
|
301
|
-
*
|
|
302
289
|
* @param rootElement The root HTML element of the PDFViewer component.
|
|
303
290
|
* @param pageNumber The page number.
|
|
291
|
+
* @example
|
|
292
|
+
* ```jsx
|
|
293
|
+
* function App() {
|
|
294
|
+
* const pdfRef = React.useRef(null);
|
|
295
|
+
* const handleClick = () => {
|
|
296
|
+
* scrollToPage(pdfRef.current.element, 3);
|
|
297
|
+
* };
|
|
298
|
+
* return (
|
|
299
|
+
* <div>
|
|
300
|
+
* <Button onClick={handleClick} >
|
|
301
|
+
* Scroll to Page 3
|
|
302
|
+
* </Button>
|
|
303
|
+
* <PDFViewer
|
|
304
|
+
* ref={pdfRef}
|
|
305
|
+
* />
|
|
306
|
+
* </div>
|
|
307
|
+
* )
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
304
310
|
*/
|
|
305
311
|
export const scrollToPage = (rootElement, pageNumber) => {
|
|
306
312
|
const pages = rootElement.querySelectorAll('.k-page');
|
|
@@ -329,3 +335,82 @@ export const currentPage = (rootElement) => {
|
|
|
329
335
|
0.01)
|
|
330
336
|
: 0;
|
|
331
337
|
};
|
|
338
|
+
/**
|
|
339
|
+
* @hidden
|
|
340
|
+
*
|
|
341
|
+
* related to https://github.com/telerik/kendo-pdfviewer-common/issues/6
|
|
342
|
+
* the bigger the canvas size, the worse the performance;
|
|
343
|
+
* if initial size after scaling is greater than browser limits,
|
|
344
|
+
* we are limiting it to the limits, then halving it for performance.
|
|
345
|
+
*/
|
|
346
|
+
const adjustCanvasSize = (targetWidth, targetHeight) => {
|
|
347
|
+
const { maxWidth, maxHeight, maxArea } = isFirefox(navigator.userAgent) ?
|
|
348
|
+
{ maxWidth: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxHeight: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxArea: MAX_CANVAS_AREA_FIREFOX } :
|
|
349
|
+
isSafari(navigator.userAgent) ?
|
|
350
|
+
{ maxWidth: MAX_CANVAS_WIDTH_SAFARI, maxHeight: MAX_CANVAS_HEIGHT_SAFARI, maxArea: MAX_CANVAS_AREA_CHROME_SAFARI } :
|
|
351
|
+
{
|
|
352
|
+
maxWidth: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
353
|
+
maxHeight: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
354
|
+
maxArea: MAX_CANVAS_AREA_CHROME_SAFARI
|
|
355
|
+
};
|
|
356
|
+
let adjustedWidth = targetWidth;
|
|
357
|
+
let adjustedHeight = targetHeight;
|
|
358
|
+
const ratio = targetWidth / targetHeight;
|
|
359
|
+
if (targetWidth > maxWidth) {
|
|
360
|
+
adjustedWidth = maxWidth;
|
|
361
|
+
const deltaWidth = targetWidth - maxWidth;
|
|
362
|
+
const deltaHeight = deltaWidth / ratio;
|
|
363
|
+
adjustedHeight = targetHeight - deltaHeight;
|
|
364
|
+
}
|
|
365
|
+
if (adjustedHeight > maxHeight) {
|
|
366
|
+
const deltaHeight = adjustedHeight - maxHeight;
|
|
367
|
+
const deltaWidth = deltaHeight * ratio;
|
|
368
|
+
adjustedHeight = maxHeight;
|
|
369
|
+
adjustedWidth -= deltaWidth;
|
|
370
|
+
}
|
|
371
|
+
const adjustedArea = adjustedWidth * adjustedHeight;
|
|
372
|
+
if (adjustedArea > maxArea) {
|
|
373
|
+
const areaRatio = Math.sqrt(maxArea / adjustedArea);
|
|
374
|
+
adjustedWidth *= areaRatio;
|
|
375
|
+
adjustedHeight *= areaRatio;
|
|
376
|
+
}
|
|
377
|
+
const adjustRatio = adjustedWidth / targetWidth;
|
|
378
|
+
return {
|
|
379
|
+
adjustedWidth: adjustedWidth !== targetWidth ? Math.floor(adjustedWidth / 2) : targetWidth,
|
|
380
|
+
adjustedHeight: adjustedHeight !== targetHeight ? Math.floor(adjustedHeight / 2) : targetHeight,
|
|
381
|
+
adjustRatio: adjustRatio !== 1 ? adjustRatio / 2 : 1
|
|
382
|
+
};
|
|
383
|
+
};
|
|
384
|
+
const createCanvas = (page, zoom = 1, cssClass = '') => {
|
|
385
|
+
const scaleNum = scale();
|
|
386
|
+
const viewport = page.getViewport({ scale: scaleNum });
|
|
387
|
+
const { adjustedWidth, adjustedHeight, adjustRatio } = adjustCanvasSize(viewport.width, viewport.height);
|
|
388
|
+
const styles = {
|
|
389
|
+
width: Math.floor(viewport.width / scaleNum) * zoom + 'pt',
|
|
390
|
+
height: Math.floor(viewport.height / scaleNum) * zoom + 'pt'
|
|
391
|
+
};
|
|
392
|
+
const pageElement = createElement('div', cssClass, styles);
|
|
393
|
+
const canvas = createElement('canvas', '', {
|
|
394
|
+
width: '100%',
|
|
395
|
+
height: '100%'
|
|
396
|
+
});
|
|
397
|
+
canvas.height = adjustedHeight;
|
|
398
|
+
canvas.width = adjustedWidth;
|
|
399
|
+
const canvasContext = canvas.getContext('2d');
|
|
400
|
+
pageElement.appendChild(canvas);
|
|
401
|
+
const adjustedScale = adjustRatio * scaleNum;
|
|
402
|
+
viewport.width = adjustedWidth;
|
|
403
|
+
viewport.height = adjustedHeight;
|
|
404
|
+
viewport.scale = adjustedScale;
|
|
405
|
+
viewport.transform[0] = adjustedScale;
|
|
406
|
+
viewport.transform[3] = -adjustedScale;
|
|
407
|
+
viewport.transform[5] = adjustedHeight;
|
|
408
|
+
return {
|
|
409
|
+
canvasContext,
|
|
410
|
+
viewport,
|
|
411
|
+
scaleNum,
|
|
412
|
+
canvas,
|
|
413
|
+
pageElement,
|
|
414
|
+
styles
|
|
415
|
+
};
|
|
416
|
+
};
|
package/dist/npm/utils.d.ts
CHANGED
|
@@ -82,9 +82,27 @@ export declare const goToPreviousSearchMatch: (ref: any) => void;
|
|
|
82
82
|
export declare const calculateZoomLevel: (zoomLevel: number, zoomLevelType: string, currentZoom: number, dom: HTMLDivElement) => number;
|
|
83
83
|
/**
|
|
84
84
|
* Scrolls the PDFViewer document to the passed page number.
|
|
85
|
-
*
|
|
86
85
|
* @param rootElement The root HTML element of the PDFViewer component.
|
|
87
86
|
* @param pageNumber The page number.
|
|
87
|
+
* @example
|
|
88
|
+
* ```jsx
|
|
89
|
+
* function App() {
|
|
90
|
+
* const pdfRef = React.useRef(null);
|
|
91
|
+
* const handleClick = () => {
|
|
92
|
+
* scrollToPage(pdfRef.current.element, 3);
|
|
93
|
+
* };
|
|
94
|
+
* return (
|
|
95
|
+
* <div>
|
|
96
|
+
* <Button onClick={handleClick} >
|
|
97
|
+
* Scroll to Page 3
|
|
98
|
+
* </Button>
|
|
99
|
+
* <PDFViewer
|
|
100
|
+
* ref={pdfRef}
|
|
101
|
+
* />
|
|
102
|
+
* </div>
|
|
103
|
+
* )
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
88
106
|
*/
|
|
89
107
|
export declare const scrollToPage: (rootElement: HTMLElement, pageNumber: number) => void;
|
|
90
108
|
/**
|
package/dist/npm/utils.js
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const kendo_file_saver_1 = require("@progress/kendo-file-saver");
|
|
4
|
+
const kendo_common_1 = require("@progress/kendo-common");
|
|
4
5
|
const pdfjs_dist_1 = require("pdfjs-dist");
|
|
6
|
+
const MAX_CANVAS_WIDTH_HEIGHT_CHROME = 65535;
|
|
7
|
+
const MAX_CANVAS_AREA_CHROME_SAFARI = 268435456;
|
|
8
|
+
const MAX_CANVAS_WIDTH_HEIGHT_FIREFOX = 32767;
|
|
9
|
+
const MAX_CANVAS_AREA_FIREFOX = 124992400;
|
|
10
|
+
const MAX_CANVAS_WIDTH_SAFARI = 4194303;
|
|
11
|
+
const MAX_CANVAS_HEIGHT_SAFARI = 8388607;
|
|
5
12
|
const isIOS = () => /iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.maxTouchPoints &&
|
|
6
13
|
navigator.maxTouchPoints > 2 &&
|
|
7
14
|
/Macintosh/i.test(navigator.userAgent));
|
|
15
|
+
const isSafari = (userAgent) => {
|
|
16
|
+
return kendo_common_1.detectDesktopBrowser(userAgent).safari ||
|
|
17
|
+
(kendo_common_1.detectMobileOS(userAgent) && kendo_common_1.detectMobileOS(userAgent).browser === 'mobilesafari');
|
|
18
|
+
};
|
|
19
|
+
const isFirefox = (userAgent) => {
|
|
20
|
+
const desktopBrowser = kendo_common_1.detectDesktopBrowser(userAgent);
|
|
21
|
+
const mobileOS = kendo_common_1.detectMobileOS(userAgent);
|
|
22
|
+
return (desktopBrowser && desktopBrowser.mozilla) || (mobileOS && mobileOS.browser === 'firefox');
|
|
23
|
+
};
|
|
8
24
|
/**
|
|
9
25
|
* @hidden
|
|
10
26
|
*/
|
|
@@ -154,21 +170,7 @@ const openPrintDialog = (dom, width, height, done, onError) => {
|
|
|
154
170
|
}
|
|
155
171
|
};
|
|
156
172
|
const renderCanvas = (page, done, error) => {
|
|
157
|
-
const scaleNum =
|
|
158
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
159
|
-
const styles = {
|
|
160
|
-
width: Math.floor(viewport.width / scaleNum) + 'pt',
|
|
161
|
-
height: Math.floor(viewport.height / scaleNum) + 'pt'
|
|
162
|
-
};
|
|
163
|
-
const pageElement = createElement('div', '', styles);
|
|
164
|
-
const canvas = createElement('canvas', '', {
|
|
165
|
-
width: '100%',
|
|
166
|
-
height: '100%'
|
|
167
|
-
});
|
|
168
|
-
canvas.height = viewport.height;
|
|
169
|
-
canvas.width = viewport.width;
|
|
170
|
-
const canvasContext = canvas.getContext('2d');
|
|
171
|
-
pageElement.appendChild(canvas);
|
|
173
|
+
const { canvasContext, viewport, scaleNum, canvas, pageElement } = createCanvas(page);
|
|
172
174
|
page.render({ canvasContext, viewport })
|
|
173
175
|
.promise.then(() => {
|
|
174
176
|
const printContent = new Image(Math.floor((viewport.width / scaleNum)), Math.floor((viewport.height / scaleNum)));
|
|
@@ -193,21 +195,7 @@ const createElement = function (name, className, styles) {
|
|
|
193
195
|
return element;
|
|
194
196
|
};
|
|
195
197
|
const renderPage = (page, zoom, error) => {
|
|
196
|
-
const
|
|
197
|
-
const viewport = page.getViewport({ scale: scaleNum });
|
|
198
|
-
const styles = {
|
|
199
|
-
width: (Math.floor(viewport.width / scaleNum) * zoom) + 'pt',
|
|
200
|
-
height: (Math.floor(viewport.height / scaleNum) * zoom) + 'pt'
|
|
201
|
-
};
|
|
202
|
-
const pageElement = createElement('div', 'k-page', styles);
|
|
203
|
-
const canvas = createElement('canvas', '', {
|
|
204
|
-
width: '100%',
|
|
205
|
-
height: '100%'
|
|
206
|
-
});
|
|
207
|
-
canvas.height = viewport.height;
|
|
208
|
-
canvas.width = viewport.width;
|
|
209
|
-
const canvasContext = canvas.getContext('2d');
|
|
210
|
-
pageElement.appendChild(canvas);
|
|
198
|
+
const { canvasContext, viewport, pageElement, styles } = createCanvas(page, zoom, 'k-page');
|
|
211
199
|
page.render({ canvasContext, viewport })
|
|
212
200
|
.promise.then(() => {
|
|
213
201
|
page.getTextContent().then((textContent) => {
|
|
@@ -300,9 +288,27 @@ exports.calculateZoomLevel = (zoomLevel, zoomLevelType, currentZoom, dom) => {
|
|
|
300
288
|
};
|
|
301
289
|
/**
|
|
302
290
|
* Scrolls the PDFViewer document to the passed page number.
|
|
303
|
-
*
|
|
304
291
|
* @param rootElement The root HTML element of the PDFViewer component.
|
|
305
292
|
* @param pageNumber The page number.
|
|
293
|
+
* @example
|
|
294
|
+
* ```jsx
|
|
295
|
+
* function App() {
|
|
296
|
+
* const pdfRef = React.useRef(null);
|
|
297
|
+
* const handleClick = () => {
|
|
298
|
+
* scrollToPage(pdfRef.current.element, 3);
|
|
299
|
+
* };
|
|
300
|
+
* return (
|
|
301
|
+
* <div>
|
|
302
|
+
* <Button onClick={handleClick} >
|
|
303
|
+
* Scroll to Page 3
|
|
304
|
+
* </Button>
|
|
305
|
+
* <PDFViewer
|
|
306
|
+
* ref={pdfRef}
|
|
307
|
+
* />
|
|
308
|
+
* </div>
|
|
309
|
+
* )
|
|
310
|
+
* }
|
|
311
|
+
* ```
|
|
306
312
|
*/
|
|
307
313
|
exports.scrollToPage = (rootElement, pageNumber) => {
|
|
308
314
|
const pages = rootElement.querySelectorAll('.k-page');
|
|
@@ -331,3 +337,82 @@ exports.currentPage = (rootElement) => {
|
|
|
331
337
|
0.01)
|
|
332
338
|
: 0;
|
|
333
339
|
};
|
|
340
|
+
/**
|
|
341
|
+
* @hidden
|
|
342
|
+
*
|
|
343
|
+
* related to https://github.com/telerik/kendo-pdfviewer-common/issues/6
|
|
344
|
+
* the bigger the canvas size, the worse the performance;
|
|
345
|
+
* if initial size after scaling is greater than browser limits,
|
|
346
|
+
* we are limiting it to the limits, then halving it for performance.
|
|
347
|
+
*/
|
|
348
|
+
const adjustCanvasSize = (targetWidth, targetHeight) => {
|
|
349
|
+
const { maxWidth, maxHeight, maxArea } = isFirefox(navigator.userAgent) ?
|
|
350
|
+
{ maxWidth: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxHeight: MAX_CANVAS_WIDTH_HEIGHT_FIREFOX, maxArea: MAX_CANVAS_AREA_FIREFOX } :
|
|
351
|
+
isSafari(navigator.userAgent) ?
|
|
352
|
+
{ maxWidth: MAX_CANVAS_WIDTH_SAFARI, maxHeight: MAX_CANVAS_HEIGHT_SAFARI, maxArea: MAX_CANVAS_AREA_CHROME_SAFARI } :
|
|
353
|
+
{
|
|
354
|
+
maxWidth: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
355
|
+
maxHeight: MAX_CANVAS_WIDTH_HEIGHT_CHROME,
|
|
356
|
+
maxArea: MAX_CANVAS_AREA_CHROME_SAFARI
|
|
357
|
+
};
|
|
358
|
+
let adjustedWidth = targetWidth;
|
|
359
|
+
let adjustedHeight = targetHeight;
|
|
360
|
+
const ratio = targetWidth / targetHeight;
|
|
361
|
+
if (targetWidth > maxWidth) {
|
|
362
|
+
adjustedWidth = maxWidth;
|
|
363
|
+
const deltaWidth = targetWidth - maxWidth;
|
|
364
|
+
const deltaHeight = deltaWidth / ratio;
|
|
365
|
+
adjustedHeight = targetHeight - deltaHeight;
|
|
366
|
+
}
|
|
367
|
+
if (adjustedHeight > maxHeight) {
|
|
368
|
+
const deltaHeight = adjustedHeight - maxHeight;
|
|
369
|
+
const deltaWidth = deltaHeight * ratio;
|
|
370
|
+
adjustedHeight = maxHeight;
|
|
371
|
+
adjustedWidth -= deltaWidth;
|
|
372
|
+
}
|
|
373
|
+
const adjustedArea = adjustedWidth * adjustedHeight;
|
|
374
|
+
if (adjustedArea > maxArea) {
|
|
375
|
+
const areaRatio = Math.sqrt(maxArea / adjustedArea);
|
|
376
|
+
adjustedWidth *= areaRatio;
|
|
377
|
+
adjustedHeight *= areaRatio;
|
|
378
|
+
}
|
|
379
|
+
const adjustRatio = adjustedWidth / targetWidth;
|
|
380
|
+
return {
|
|
381
|
+
adjustedWidth: adjustedWidth !== targetWidth ? Math.floor(adjustedWidth / 2) : targetWidth,
|
|
382
|
+
adjustedHeight: adjustedHeight !== targetHeight ? Math.floor(adjustedHeight / 2) : targetHeight,
|
|
383
|
+
adjustRatio: adjustRatio !== 1 ? adjustRatio / 2 : 1
|
|
384
|
+
};
|
|
385
|
+
};
|
|
386
|
+
const createCanvas = (page, zoom = 1, cssClass = '') => {
|
|
387
|
+
const scaleNum = scale();
|
|
388
|
+
const viewport = page.getViewport({ scale: scaleNum });
|
|
389
|
+
const { adjustedWidth, adjustedHeight, adjustRatio } = adjustCanvasSize(viewport.width, viewport.height);
|
|
390
|
+
const styles = {
|
|
391
|
+
width: Math.floor(viewport.width / scaleNum) * zoom + 'pt',
|
|
392
|
+
height: Math.floor(viewport.height / scaleNum) * zoom + 'pt'
|
|
393
|
+
};
|
|
394
|
+
const pageElement = createElement('div', cssClass, styles);
|
|
395
|
+
const canvas = createElement('canvas', '', {
|
|
396
|
+
width: '100%',
|
|
397
|
+
height: '100%'
|
|
398
|
+
});
|
|
399
|
+
canvas.height = adjustedHeight;
|
|
400
|
+
canvas.width = adjustedWidth;
|
|
401
|
+
const canvasContext = canvas.getContext('2d');
|
|
402
|
+
pageElement.appendChild(canvas);
|
|
403
|
+
const adjustedScale = adjustRatio * scaleNum;
|
|
404
|
+
viewport.width = adjustedWidth;
|
|
405
|
+
viewport.height = adjustedHeight;
|
|
406
|
+
viewport.scale = adjustedScale;
|
|
407
|
+
viewport.transform[0] = adjustedScale;
|
|
408
|
+
viewport.transform[3] = -adjustedScale;
|
|
409
|
+
viewport.transform[5] = adjustedHeight;
|
|
410
|
+
return {
|
|
411
|
+
canvasContext,
|
|
412
|
+
viewport,
|
|
413
|
+
scaleNum,
|
|
414
|
+
canvas,
|
|
415
|
+
pageElement,
|
|
416
|
+
styles
|
|
417
|
+
};
|
|
418
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@progress/kendo-pdfviewer-common",
|
|
3
3
|
"description": "Kendo UI TypeScript package exporting functions for PDFViewer component",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.3-dev.202310060852",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Kendo UI"
|
|
7
7
|
],
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"@progress/kendo-file-saver": "^1.1.1"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"
|
|
31
|
+
"@progress/kendo-common": "^0.2.2",
|
|
32
|
+
"pdfjs-dist": "3.11.174",
|
|
32
33
|
"tslib": "^2.4.1"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|