-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPDFViewer.tsx
More file actions
206 lines (190 loc) · 6.23 KB
/
PDFViewer.tsx
File metadata and controls
206 lines (190 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useEffect, useState, useRef } from "react";
import { PDFJsProps, PDFViewerProps } from "./PDFViewer.types";
import { Loading } from "../Loading";
import { Slugify } from "./Slugify";
import { ResolvePDFSource } from "./ResolvePDFSource";
const PDFViewer: React.FC<PDFViewerProps> = (props) => {
const viewerRef = React.useRef<HTMLDivElement | null>(null);
const {
iframeId,
src,
viewerLocation,
documentName,
documentNameColour,
backend,
toolbar = "minimal",
additionalBackendAttributes,
// optional: lets callers skip client-side fetch for remote resources
disableClientFetch = false,
...attributes
} = props;
const [loading, setLoading] = useState<boolean>(false);
// ref for object URL (either created by ResolvePDFSource or by us after fetching)
const objectUrlRef = useRef<string | null>(null);
// did ResolvePDFSource create the object URL? (helps understanding where to revoke)
const createdByResolveRef = useRef<boolean>(false);
// fetch abort controller for cross-origin remote fetches
const fetchControllerRef = useRef<AbortController | null>(null);
const isRemoteUrl = (url?: string | null) => {
if (!url) return false;
// Check if it's an HTTP(S) URL
return /^https?:\/\//i.test(url);
};
useEffect(() => {
let mounted = true;
setLoading(true);
const element = viewerRef.current;
if (!element) {
setLoading(false);
return;
}
// cleanup previous URL/fetch if any
if (fetchControllerRef.current) {
try {
fetchControllerRef.current.abort();
} catch (err) {
console.error("PDFViewer: fetch abort error:", err);
/* ignore */
}
fetchControllerRef.current = null;
}
if (objectUrlRef.current) {
try {
URL.revokeObjectURL(objectUrlRef.current);
} catch (err) {
console.error("PDFViewer: revokeObjectURL error:", err);
}
objectUrlRef.current = null;
createdByResolveRef.current = false;
}
const init = async () => {
let sourceToUse: string | undefined = src;
// 1) Let ResolvePDFSource handle base64/data/blob inputs synchronously.
try {
const resolved = ResolvePDFSource(src);
if (resolved?.source) {
sourceToUse = resolved.source;
if (resolved.isBase64Source) {
// ResolvePDFSource created an object URL for us (data/blob/base64)
objectUrlRef.current = resolved.source;
createdByResolveRef.current = true;
}
}
} catch (err) {
// If ResolvePDFSource throws, just ignore and fall back to src
console.error("PDFViewer: ResolvePDFSource error:", err);
}
// 2) If ResolvePDFSource didn't create an object URL and the src is cross-origin HTTP(S),
// attempt a client-side fetch to create an object URL (so viewer can load it safely).
if (
!objectUrlRef.current &&
!disableClientFetch &&
typeof src === "string" &&
/^https?:\/\//i.test(src) &&
isRemoteUrl(src)
) {
const controller = new AbortController();
fetchControllerRef.current = controller;
try {
const resp = await fetch(src, {
signal: controller.signal,
credentials: "omit",
});
if (resp.ok) {
const blob = await resp.blob();
if (blob && blob.size > 0) {
const obj = URL.createObjectURL(blob);
objectUrlRef.current = obj;
createdByResolveRef.current = false; // we created it via fetch
sourceToUse = obj;
} else {
console.warn(
"PDFViewer: fetched blob is empty - falling back to original src",
);
}
} else {
console.warn(
`PDFViewer: fetch returned ${resp.status} ${resp.statusText} for ${src}`,
);
// fallback: keep using original src
}
} catch (err: unknown) {
if (err instanceof Error && err.name === "AbortError") {
// aborted, nothing to do
if (!mounted) return;
} else {
console.error("PDFViewer: error fetching cross-origin PDF:", err);
}
// fallback to source as-is (original remote URL)
}
}
// 3) Prepare and call backend
if (!mounted) return;
const jsProps: PDFJsProps = {
iframeId,
source: sourceToUse,
viewerLocation,
documentName,
documentNameColour,
element,
toolbar,
...additionalBackendAttributes,
};
try {
backend(jsProps);
} catch (err) {
console.error("PDFViewer: backend threw an error:", err);
} finally {
if (mounted) setLoading(false);
}
}; // end init
init();
return () => {
mounted = false;
// cancel any ongoing fetch
if (fetchControllerRef.current) {
try {
fetchControllerRef.current.abort();
} catch (err) {
console.error("PDFViewer: fetch abort error during cleanup:", err);
}
fetchControllerRef.current = null;
}
// revoke any object URL we created
if (objectUrlRef.current) {
try {
URL.revokeObjectURL(objectUrlRef.current);
} catch (err) {
console.error("PDFViewer: fetch abort error during cleanup:", err);
}
objectUrlRef.current = null;
createdByResolveRef.current = false;
}
// Note: PDFViewerBackend also attaches an iframe to the element. If backend does not
// remove previous iframes, you might want to clear element.children here.
};
// re-run when these inputs change
}, [
src,
iframeId,
viewerLocation,
documentName,
documentNameColour,
toolbar,
backend,
disableClientFetch,
]);
return (
<>
{loading && <Loading message={"Loading PDF Document"} />}
<div
ref={viewerRef}
id={`viewer ${documentName ? Slugify(documentName) : ""}`}
data-testid={`viewer ${documentName ? Slugify(documentName) : ""}`}
style={{ width: "100%", height: "100%" }}
{...attributes}
/>
</>
);
};
export default PDFViewer;