diff --git a/lib/Page.tsx b/lib/Page.tsx index ae224e3..e960af9 100644 --- a/lib/Page.tsx +++ b/lib/Page.tsx @@ -9,6 +9,7 @@ import { useViewerContext } from "@/hooks/useViewerContext" import type { PageProps } from "@/types" import UltimateReactPdfError from "./components/UltimateReactPdfError" +import { isWindowDefined } from "./utils" export function Page({ className, @@ -16,7 +17,7 @@ export function Page({ initialPage = 1, annotations = true, pageRef, - viewPortScale = typeof window !== "undefined" ? window.devicePixelRatio : 1, + viewPortScale = isWindowDefined ? window.devicePixelRatio : 1, onPageChange, onPageError, onPageLoad, @@ -81,7 +82,8 @@ export function Page({ viewPortScale, ]) - if (!pdf) return null + if (!pdf && status === STATUS.LOADING) return + if (!pdf && status === STATUS.ERROR) return return (
@@ -93,9 +95,6 @@ export function Page({ /> )} - {status === STATUS.LOADING && } - {status === STATUS.ERROR && } -
{ const [status, setStatus] = useState(STATUS.LOADING) const [pdf, setPdf] = useState() - const isTaskInProgress = useRef(false) + const isTaskInProgress = useRef() useEffect(() => { if (pdf || isTaskInProgress.current) return const loadDocument = async () => { try { - isTaskInProgress.current = true + if (typeof src === "string") { + if (isValidUrl(src)) { + isTaskInProgress.current = getDocument({ + url: src, + verbosity: 0, + ...options, + }) + } - const pdfLoaded = await getDocument(src).promise + if (!isValidUrl(src)) { + isTaskInProgress.current = getDocument({ + data: isWindowDefined ? window.atob(src) : src, + verbosity: 0, + ...options, + }) + } + } + + if (src instanceof URL) { + isTaskInProgress.current = getDocument({ + url: src, + verbosity: 0, + ...options, + }) + } + + if (!isTaskInProgress.current) + throw new UltimateReactPdfError("Unsupported source") + + const pdfLoaded = await isTaskInProgress.current.promise onDocumentLoad && onDocumentLoad(pdfLoaded) @@ -52,13 +82,11 @@ export const PdfViewerProvider = ({ setStatus(STATUS.ERROR) throw error - } finally { - isTaskInProgress.current = false } } loadDocument() - }, [src, pdf, onDocumentError, onDocumentLoad]) + }, [src, pdf, onDocumentError, onDocumentLoad, options]) return ( { children: React.ReactNode } @@ -46,6 +50,7 @@ export interface DocumentProps { onDocumentError?: (error: unknown) => void onDocumentLoad?: (document: PDFDocumentProxy | undefined) => void messages?: Messages + options?: Omit } export interface PageCommonProps { diff --git a/lib/utils.ts b/lib/utils.ts new file mode 100644 index 0000000..540c5e6 --- /dev/null +++ b/lib/utils.ts @@ -0,0 +1,15 @@ +export const isWindowDefined = typeof window !== "undefined" + +export const isValidUrl = (url: string) => { + if (URL.canParse !== undefined) { + return URL.canParse(url) + } + + // Fallback for older browsers versions + try { + new URL(url) + return true + } catch { + return false + } +} diff --git a/src/main.tsx b/src/main.tsx index cd0dcda..9b7ab31 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -10,7 +10,7 @@ import { examplePdf } from "./example" ReactDOM.createRoot(document.getElementById("root")!).render( - +