@react-pdf-kit/viewer 2.1.0-beta.1 → 2.1.0-beta.3

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.
@@ -1,6 +1,6 @@
1
- import { useState as g, useRef as N, useEffect as R, useCallback as P } from "react";
2
- import { useDocumentContext as O } from "../../contexts/RPDocumentContext.js";
3
- import { PixelsPerInch as X, AnnotationMode as q, RenderingCancelledException as B } from "pdfjs-dist";
1
+ import { useState as g, useRef as k, useEffect as D, useCallback as P } from "react";
2
+ import { useDocumentContext as q } from "../../contexts/RPDocumentContext.js";
3
+ import { PixelsPerInch as V, AnnotationMode as O, RenderingCancelledException as U } from "pdfjs-dist";
4
4
  import "react/jsx-runtime";
5
5
  import "./useLoadPdf.js";
6
6
  import "../types.js";
@@ -13,118 +13,111 @@ import "./usePdfProperties.js";
13
13
  import "../convertPdfDate.js";
14
14
  import "../formatFileSize.js";
15
15
  const re = () => {
16
- const { pdf: l, pages: f } = O(), [D, E] = g({
16
+ const { pdf: l, pages: f } = q(), [R, E] = g({
17
17
  loadedPages: 0,
18
18
  totalPages: 0,
19
19
  percentage: 0
20
- }), [T, b] = g(!1), [A, y] = g(!1), [Z, L] = g(null), [F, w] = g(void 0), s = N(!1);
21
- R(() => {
22
- s.current;
23
- }, [s]);
24
- const C = P(() => {
20
+ }), [T, y] = g(!1), [A, b] = g(!1), [Z, C] = g(null), [M, w] = g(void 0), i = k(!1);
21
+ D(() => {
22
+ i.current;
23
+ }, [i]);
24
+ const S = P(() => {
25
25
  document.documentElement.classList.remove("rp-print-html-printing"), document.body.classList.remove("rp-print-body-printing");
26
26
  const e = document.querySelector(".rp-print-zone");
27
27
  e && e.remove();
28
- }, []), a = P(() => {
29
- s.current = !1, b(!1), y(!1), L(null), w(void 0), E({
28
+ }, []), s = P(() => {
29
+ i.current = !1, y(!1), b(!1), C(null), w(void 0), E({
30
30
  loadedPages: 0,
31
31
  totalPages: 0,
32
32
  percentage: 0
33
33
  });
34
- }, []), M = () => {
35
- s.current = !0;
34
+ }, []), F = () => {
35
+ i.current = !0;
36
36
  }, I = async ({
37
37
  scratchCanvas: e,
38
38
  pdfDocument: n,
39
39
  pageNumber: t,
40
- viewerPdfPage: o,
41
- printResolution: r,
42
- optionalContentConfigPromise: i,
40
+ viewerPdfPage: r,
41
+ printResolution: o,
42
+ optionalContentConfigPromise: c,
43
43
  printAnnotationStoragePromise: u
44
44
  }) => {
45
- if (s.current) {
46
- a();
45
+ if (i.current) {
46
+ s();
47
47
  return;
48
48
  }
49
- const c = r / X.PDF;
50
- e.width = Math.floor(o.width * c), e.height = Math.floor(o.height * c);
49
+ const a = o / V.PDF;
50
+ e.width = Math.floor(r.width * a), e.height = Math.floor(r.height * a);
51
51
  const p = e.getContext("2d");
52
52
  p.save(), p.fillStyle = "rgb(255, 255, 255)", p.fillRect(0, 0, e.width, e.height), p.restore();
53
53
  const [m, h] = await Promise.all([
54
54
  n.getPage(t),
55
55
  u
56
56
  ]);
57
- if (s.current) {
58
- a();
57
+ if (i.current) {
58
+ s();
59
59
  return;
60
60
  }
61
61
  const v = {
62
62
  canvas: e,
63
63
  canvasContext: p,
64
- transform: [c, 0, 0, c, 0, 0],
65
- viewport: m.getViewport({ scale: 1, rotation: o.rotation }),
64
+ transform: [a, 0, 0, a, 0, 0],
65
+ viewport: m.getViewport({ scale: 1, rotation: r.rotation }),
66
66
  intent: "print",
67
- annotationMode: q.ENABLE_STORAGE,
68
- optionalContentConfigPromise: i,
67
+ annotationMode: O.ENABLE_STORAGE,
68
+ optionalContentConfigPromise: c,
69
69
  printAnnotationStorage: h
70
70
  };
71
71
  try {
72
72
  return m.render(v).promise;
73
73
  } catch (d) {
74
- throw d instanceof B || console.error(d), d;
74
+ throw d instanceof U || console.error(d), d;
75
75
  }
76
- }, S = P(() => {
76
+ }, L = P(() => {
77
77
  const e = window.print;
78
78
  return new Promise((n) => {
79
- const t = (i) => {
79
+ const t = (c) => {
80
80
  setTimeout(() => {
81
- e.call(window), setTimeout(() => i(), 20);
81
+ e.call(window), setTimeout(() => c(), 20);
82
82
  }, 0);
83
83
  };
84
84
  if (document.querySelector(".rp-print-zone")) {
85
85
  t(n);
86
86
  return;
87
87
  }
88
- const r = new MutationObserver(() => {
89
- document.querySelector(".rp-print-zone") && (t(n), r.disconnect());
88
+ const o = new MutationObserver(() => {
89
+ document.querySelector(".rp-print-zone") && (t(n), o.disconnect());
90
90
  });
91
- r.observe(document.body, { childList: !0, subtree: !0 });
91
+ o.observe(document.body, { childList: !0, subtree: !0 });
92
92
  });
93
- }, []), U = async (e) => {
93
+ }, []), z = (e) => {
94
94
  const n = document.createElement("img"), t = document.createElement("div");
95
- return t.classList.add("rp-print-page"), await new Promise((o, r) => {
96
- e.toBlob((i) => {
97
- i ? (n.src = URL.createObjectURL(i), n.onload = () => {
98
- URL.revokeObjectURL(n.src), o();
99
- }, n.onerror = r) : r(new Error("Failed to create blob"));
100
- });
101
- }).catch(() => {
102
- }), t.appendChild(n), t;
95
+ return t.classList.add("rp-print-page"), n.src = e.toDataURL(), t.appendChild(n), t;
103
96
  }, x = (e, n) => {
104
97
  const t = document.createElement("div");
105
98
  t.classList.add("rp-print-zone");
106
- const o = document.createElement("style"), r = n.get(1);
107
- if (r) {
108
- const { width: i, height: u } = r == null ? void 0 : r.page.getViewport({ scale: 1 });
109
- o.innerHTML = `@page { size: ${i}px ${u}px }`;
99
+ const r = document.createElement("style"), o = n.get(1);
100
+ if (o) {
101
+ const c = o.page.getViewport({ scale: 1 }), { width: u, height: a } = c;
102
+ r.innerHTML = `@page { size: ${u}pt ${a}pt; margin: 0; }`;
110
103
  }
111
- return t.appendChild(o), t.append(...e), t;
112
- }, k = P(
104
+ return t.appendChild(r), t.append(...e), t;
105
+ }, N = P(
113
106
  async (e) => {
114
107
  if (!l)
115
108
  return;
116
- a();
109
+ s();
117
110
  const n = e && "visibleDefaultProgress" in e ? e : void 0;
118
111
  (n == null ? void 0 : n.visibleDefaultProgress) !== void 0 ? w(n.visibleDefaultProgress) : w(!0);
119
- const t = document.createElement("canvas"), o = [];
112
+ const t = document.createElement("canvas"), r = [];
120
113
  try {
121
114
  l.isPureXfa && console.warn("[rp] XFA Form is not supported at the moment.");
122
- const i = l.getOptionalContentConfig({ intent: "print" }), u = Promise.resolve(
115
+ const c = l.getOptionalContentConfig({ intent: "print" }), u = Promise.resolve(
123
116
  l.annotationStorage.print ?? void 0
124
- ), c = f.size;
117
+ ), a = f.size;
125
118
  for (const [m, h] of f) {
126
- if (s.current) {
127
- a();
119
+ if (i.current) {
120
+ s();
128
121
  return;
129
122
  }
130
123
  const v = h.page.getViewport({ scale: 1 });
@@ -134,50 +127,50 @@ const re = () => {
134
127
  pageNumber: m,
135
128
  viewerPdfPage: v,
136
129
  printResolution: 150,
137
- optionalContentConfigPromise: i,
130
+ optionalContentConfigPromise: c,
138
131
  printAnnotationStoragePromise: u
139
- }), s.current) {
140
- a();
132
+ }), i.current) {
133
+ s();
141
134
  return;
142
135
  }
143
- const d = await U(t);
144
- o.push(d);
145
- const z = parseFloat((o.length / c * 100).toFixed(2));
136
+ const d = z(t);
137
+ r.push(d);
138
+ const X = parseFloat((r.length / a * 100).toFixed(2));
146
139
  E({
147
140
  loadedPages: m,
148
- totalPages: c,
149
- percentage: z
141
+ totalPages: a,
142
+ percentage: X
150
143
  });
151
144
  }
152
- if (s.current) {
153
- a();
145
+ if (i.current) {
146
+ s();
154
147
  return;
155
148
  }
156
- const p = x(o, f);
157
- document.body.appendChild(p), document.documentElement.classList.add("rp-print-html-printing"), document.body.classList.add("rp-print-body-printing"), b(!0), await S();
158
- } catch (r) {
159
- y(!0), r instanceof Error && L(r);
149
+ const p = x(r, f);
150
+ document.body.appendChild(p), document.documentElement.classList.add("rp-print-html-printing"), document.body.classList.add("rp-print-body-printing"), y(!0), await L();
151
+ } catch (o) {
152
+ b(!0), o instanceof Error && C(o);
160
153
  } finally {
161
154
  t.height = 0, t.width = 0;
162
155
  }
163
156
  },
164
- [l, f, s, a, x, S]
157
+ [l, f, i, s, x, L]
165
158
  );
166
- return R(() => {
159
+ return D(() => {
167
160
  const e = () => {
168
- a(), C();
161
+ s(), S();
169
162
  };
170
163
  return window.addEventListener("afterprint", e, !0), () => {
171
164
  window.removeEventListener("afterprint", e, !0);
172
165
  };
173
- }, [C, a]), {
174
- print: k,
175
- cancel: M,
176
- progress: D,
166
+ }, [S, s]), {
167
+ print: N,
168
+ cancel: F,
169
+ progress: R,
177
170
  isComplete: T,
178
171
  isError: A,
179
172
  error: Z,
180
- showDefaultProgress: F
173
+ showDefaultProgress: M
181
174
  };
182
175
  };
183
176
  export {
@@ -3,17 +3,18 @@ function e() {
3
3
  return;
4
4
  const t = document.createElement("style");
5
5
  t.id = "rp-print-style", t.textContent = `
6
+ @page { margin: 0; }
7
+
6
8
  .rp-print-zone { display: none; }
7
9
 
8
10
  @media print {
9
- @page { margin: 0; }
10
11
  .rp-print-html-printing { height: 100%; }
11
- .rp-print-body-printing { background: transparent; height: 100%; margin: 0; display: block; }
12
+ .rp-print-body-printing { background: rgb(0 0 0 / 0) none; height: 100% !important; margin: 0 !important; display: block !important; }
12
13
  .rp-print-body-printing * { display: none; }
13
14
  .rp-print-zone { display: block; height: 100%; }
14
15
  .rp-print-page { display: flex; align-items: center; flex-direction: column; justify-content: center; height: 100%; width: 100%; page-break-after: always; page-break-inside: avoid; }
15
16
  .rp-print-page:last-child { page-break-after: auto; }
16
- .rp-print-page img, .rp-print-page canvas { display: block; max-height: 100%; max-width: 100%; direction: ltr; }
17
+ .rp-print-page img, .rp-print-page canvas { display: block; max-width: 100%; max-height: 100%; direction: ltr; }
17
18
  }
18
19
  `, document.head.appendChild(t);
19
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pdf-kit/viewer",
3
- "version": "2.1.0-beta.1",
3
+ "version": "2.1.0-beta.3",
4
4
  "private": false,
5
5
  "description": "A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.",
6
6
  "keywords": [