hsu-utils 0.0.20 → 0.0.23
Sign up to get free protection for your applications and to get access to all the features.
package/es/RenderPDF/index.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
|
1
2
|
interface RenderOption {
|
2
3
|
pdfUrl: string;
|
3
4
|
containerId: string;
|
@@ -6,10 +7,14 @@ interface RenderOption {
|
|
6
7
|
pixelRatio?: number;
|
7
8
|
scale?: number;
|
8
9
|
}
|
10
|
+
declare function load(pdfUrl: string): Promise<PDFDocumentProxy>;
|
9
11
|
declare function getNumPages(pdfUrl: string): Promise<number>;
|
12
|
+
declare function clear(containerId: string): void;
|
10
13
|
declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
|
11
14
|
declare const RenderPDF: {
|
12
|
-
|
15
|
+
load: typeof load;
|
13
16
|
getNumPages: typeof getNumPages;
|
17
|
+
clear: typeof clear;
|
18
|
+
render: typeof render;
|
14
19
|
};
|
15
20
|
export default RenderPDF;
|
package/es/RenderPDF/index.js
CHANGED
@@ -10,6 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist/legacy/build/pdf.js';
|
11
11
|
GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
|
12
12
|
const PDFMap = new Map();
|
13
|
+
function load(pdfUrl) {
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
15
|
+
let pdf = PDFMap.get(pdfUrl);
|
16
|
+
if (!pdf) {
|
17
|
+
const loadingTask = getDocument({
|
18
|
+
url: pdfUrl,
|
19
|
+
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
20
|
+
cMapPacked: true
|
21
|
+
});
|
22
|
+
pdf = loadingTask.promise;
|
23
|
+
PDFMap.set(pdfUrl, pdf);
|
24
|
+
}
|
25
|
+
return pdf;
|
26
|
+
});
|
27
|
+
}
|
28
|
+
function getNumPages(pdfUrl) {
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
30
|
+
const pdf = yield load(pdfUrl);
|
31
|
+
return pdf.numPages;
|
32
|
+
});
|
33
|
+
}
|
13
34
|
function clear(containerId) {
|
14
35
|
const container = document.getElementById(containerId);
|
15
36
|
const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
|
@@ -17,6 +38,20 @@ function clear(containerId) {
|
|
17
38
|
container === null || container === void 0 ? void 0 : container.removeChild(item);
|
18
39
|
});
|
19
40
|
}
|
41
|
+
function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }) {
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
43
|
+
clear(containerId);
|
44
|
+
const container = document.getElementById(containerId);
|
45
|
+
if (!container)
|
46
|
+
return;
|
47
|
+
const pdf = yield load(pdfUrl);
|
48
|
+
const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
|
49
|
+
const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
|
50
|
+
for (let i = start; i <= end; i++) {
|
51
|
+
renderPage({ pdf, container: container, num: i, pixelRatio, scale });
|
52
|
+
}
|
53
|
+
});
|
54
|
+
}
|
20
55
|
function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
|
21
56
|
pdf.getPage(num).then((page) => {
|
22
57
|
const pageDiv = document.createElement('div');
|
@@ -39,46 +74,10 @@ function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
|
|
39
74
|
page.render(renderContext);
|
40
75
|
});
|
41
76
|
}
|
42
|
-
function getNumPages(pdfUrl) {
|
43
|
-
return __awaiter(this, void 0, void 0, function* () {
|
44
|
-
let pdf = PDFMap.get(pdfUrl);
|
45
|
-
if (!pdf) {
|
46
|
-
const loadingTask = getDocument({
|
47
|
-
url: pdfUrl,
|
48
|
-
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
49
|
-
cMapPacked: true
|
50
|
-
});
|
51
|
-
pdf = yield loadingTask.promise;
|
52
|
-
PDFMap.set(pdfUrl, pdf);
|
53
|
-
}
|
54
|
-
return pdf.numPages;
|
55
|
-
});
|
56
|
-
}
|
57
|
-
function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }) {
|
58
|
-
return __awaiter(this, void 0, void 0, function* () {
|
59
|
-
clear(containerId);
|
60
|
-
let pdf = PDFMap.get(pdfUrl);
|
61
|
-
if (!pdf) {
|
62
|
-
const loadingTask = getDocument({
|
63
|
-
url: pdfUrl,
|
64
|
-
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
65
|
-
cMapPacked: true
|
66
|
-
});
|
67
|
-
pdf = yield loadingTask.promise;
|
68
|
-
PDFMap.set(pdfUrl, pdf);
|
69
|
-
}
|
70
|
-
const container = document.getElementById(containerId);
|
71
|
-
if (!container)
|
72
|
-
return;
|
73
|
-
const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
|
74
|
-
const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
|
75
|
-
for (let i = start; i <= end; i++) {
|
76
|
-
renderPage({ pdf, container: container, num: i, pixelRatio, scale });
|
77
|
-
}
|
78
|
-
});
|
79
|
-
}
|
80
77
|
const RenderPDF = {
|
81
|
-
|
82
|
-
getNumPages
|
78
|
+
load,
|
79
|
+
getNumPages,
|
80
|
+
clear,
|
81
|
+
render
|
83
82
|
};
|
84
83
|
export default RenderPDF;
|
package/lib/RenderPDF/index.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
|
1
2
|
interface RenderOption {
|
2
3
|
pdfUrl: string;
|
3
4
|
containerId: string;
|
@@ -6,10 +7,14 @@ interface RenderOption {
|
|
6
7
|
pixelRatio?: number;
|
7
8
|
scale?: number;
|
8
9
|
}
|
10
|
+
declare function load(pdfUrl: string): Promise<PDFDocumentProxy>;
|
9
11
|
declare function getNumPages(pdfUrl: string): Promise<number>;
|
12
|
+
declare function clear(containerId: string): void;
|
10
13
|
declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
|
11
14
|
declare const RenderPDF: {
|
12
|
-
|
15
|
+
load: typeof load;
|
13
16
|
getNumPages: typeof getNumPages;
|
17
|
+
clear: typeof clear;
|
18
|
+
render: typeof render;
|
14
19
|
};
|
15
20
|
export default RenderPDF;
|
package/lib/RenderPDF/index.js
CHANGED
@@ -39,83 +39,58 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
var pdf_js_1 = require("pdfjs-dist/legacy/build/pdf.js");
|
40
40
|
pdf_js_1.GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
|
41
41
|
var PDFMap = new Map();
|
42
|
-
function
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
var ctx = canvas.getContext('2d');
|
59
|
-
var devicePixelRatio = window.devicePixelRatio * pixelRatio;
|
60
|
-
var viewport = page.getViewport({ scale: 1 * devicePixelRatio });
|
61
|
-
canvas.style.width = "calc(100% * ".concat(scale, ")");
|
62
|
-
canvas.style.height = '100%';
|
63
|
-
canvas.width = viewport.width;
|
64
|
-
canvas.height = viewport.height;
|
65
|
-
var renderContext = {
|
66
|
-
canvasContext: ctx,
|
67
|
-
viewport: viewport
|
68
|
-
};
|
69
|
-
page.render(renderContext);
|
42
|
+
function load(pdfUrl) {
|
43
|
+
return __awaiter(this, void 0, void 0, function () {
|
44
|
+
var pdf, loadingTask;
|
45
|
+
return __generator(this, function (_a) {
|
46
|
+
pdf = PDFMap.get(pdfUrl);
|
47
|
+
if (!pdf) {
|
48
|
+
loadingTask = (0, pdf_js_1.getDocument)({
|
49
|
+
url: pdfUrl,
|
50
|
+
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
51
|
+
cMapPacked: true
|
52
|
+
});
|
53
|
+
pdf = loadingTask.promise;
|
54
|
+
PDFMap.set(pdfUrl, pdf);
|
55
|
+
}
|
56
|
+
return [2, pdf];
|
57
|
+
});
|
70
58
|
});
|
71
59
|
}
|
72
60
|
function getNumPages(pdfUrl) {
|
73
61
|
return __awaiter(this, void 0, void 0, function () {
|
74
|
-
var pdf
|
62
|
+
var pdf;
|
75
63
|
return __generator(this, function (_a) {
|
76
64
|
switch (_a.label) {
|
77
|
-
case 0:
|
78
|
-
pdf = PDFMap.get(pdfUrl);
|
79
|
-
if (!!pdf) return [3, 2];
|
80
|
-
loadingTask = (0, pdf_js_1.getDocument)({
|
81
|
-
url: pdfUrl,
|
82
|
-
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
83
|
-
cMapPacked: true
|
84
|
-
});
|
85
|
-
return [4, loadingTask.promise];
|
65
|
+
case 0: return [4, load(pdfUrl)];
|
86
66
|
case 1:
|
87
67
|
pdf = _a.sent();
|
88
|
-
|
89
|
-
_a.label = 2;
|
90
|
-
case 2: return [2, pdf.numPages];
|
68
|
+
return [2, pdf.numPages];
|
91
69
|
}
|
92
70
|
});
|
93
71
|
});
|
94
72
|
}
|
73
|
+
function clear(containerId) {
|
74
|
+
var container = document.getElementById(containerId);
|
75
|
+
var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
|
76
|
+
pages === null || pages === void 0 ? void 0 : pages.forEach(function (item) {
|
77
|
+
container === null || container === void 0 ? void 0 : container.removeChild(item);
|
78
|
+
});
|
79
|
+
}
|
95
80
|
function render(_a) {
|
96
81
|
var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum, pixelRatio = _a.pixelRatio, scale = _a.scale;
|
97
82
|
return __awaiter(this, void 0, void 0, function () {
|
98
|
-
var
|
83
|
+
var container, pdf, start, end, i;
|
99
84
|
return __generator(this, function (_b) {
|
100
85
|
switch (_b.label) {
|
101
86
|
case 0:
|
102
87
|
clear(containerId);
|
103
|
-
pdf = PDFMap.get(pdfUrl);
|
104
|
-
if (!!pdf) return [3, 2];
|
105
|
-
loadingTask = (0, pdf_js_1.getDocument)({
|
106
|
-
url: pdfUrl,
|
107
|
-
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
108
|
-
cMapPacked: true
|
109
|
-
});
|
110
|
-
return [4, loadingTask.promise];
|
111
|
-
case 1:
|
112
|
-
pdf = _b.sent();
|
113
|
-
PDFMap.set(pdfUrl, pdf);
|
114
|
-
_b.label = 2;
|
115
|
-
case 2:
|
116
88
|
container = document.getElementById(containerId);
|
117
89
|
if (!container)
|
118
90
|
return [2];
|
91
|
+
return [4, load(pdfUrl)];
|
92
|
+
case 1:
|
93
|
+
pdf = _b.sent();
|
119
94
|
start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
|
120
95
|
end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
|
121
96
|
for (i = start; i <= end; i++) {
|
@@ -126,8 +101,33 @@ function render(_a) {
|
|
126
101
|
});
|
127
102
|
});
|
128
103
|
}
|
104
|
+
function renderPage(_a) {
|
105
|
+
var pdf = _a.pdf, container = _a.container, num = _a.num, _b = _a.pixelRatio, pixelRatio = _b === void 0 ? 2 : _b, _c = _a.scale, scale = _c === void 0 ? 1 : _c;
|
106
|
+
pdf.getPage(num).then(function (page) {
|
107
|
+
var pageDiv = document.createElement('div');
|
108
|
+
pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
|
109
|
+
pageDiv.setAttribute('style', 'position: relative; ');
|
110
|
+
container.appendChild(pageDiv);
|
111
|
+
var canvas = document.createElement('canvas');
|
112
|
+
pageDiv.appendChild(canvas);
|
113
|
+
var ctx = canvas.getContext('2d');
|
114
|
+
var devicePixelRatio = window.devicePixelRatio * pixelRatio;
|
115
|
+
var viewport = page.getViewport({ scale: 1 * devicePixelRatio });
|
116
|
+
canvas.style.width = "calc(100% * ".concat(scale, ")");
|
117
|
+
canvas.style.height = '100%';
|
118
|
+
canvas.width = viewport.width;
|
119
|
+
canvas.height = viewport.height;
|
120
|
+
var renderContext = {
|
121
|
+
canvasContext: ctx,
|
122
|
+
viewport: viewport
|
123
|
+
};
|
124
|
+
page.render(renderContext);
|
125
|
+
});
|
126
|
+
}
|
129
127
|
var RenderPDF = {
|
130
|
-
|
131
|
-
getNumPages: getNumPages
|
128
|
+
load: load,
|
129
|
+
getNumPages: getNumPages,
|
130
|
+
clear: clear,
|
131
|
+
render: render
|
132
132
|
};
|
133
133
|
exports.default = RenderPDF;
|