@@ -2,13 +2,6 @@ import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useSta
22import { clsx , useControllableState } from '@rozie/runtime-react' ;
33import './PdfViewer.css' ;
44import './PdfViewer.global.css' ;
5- import * as pdfjsLib from 'pdfjs-dist' ;
6-
7- // null-lets so the bundled-leaf typeNeutralize pass annotates them `any`:
8- // `instance` is the PDFDocumentProxy (whose strict types the loosely-typed props
9- // don't satisfy — routing the render chain through `any` is the maplibre
10- // mapOptions idiom), containerEl is the scroll host, observer is the
11- // continuous-mode scroll spy.
125
136interface PdfViewerProps {
147 src ?: unknown ;
@@ -18,6 +11,7 @@ interface PdfViewerProps {
1811 scale ?: number ;
1912 rotation ?: number ;
2013 workerSrc ?: string ;
14+ standardFontDataUrl ?: string ;
2115 renderAllPages ?: boolean ;
2216 textLayer ?: boolean ;
2317 password ?: unknown ;
@@ -46,23 +40,26 @@ export interface PdfViewerHandle {
4640
4741const PdfViewer = forwardRef < PdfViewerHandle , PdfViewerProps > ( function PdfViewer ( _props : PdfViewerProps , ref ) : JSX . Element {
4842 const __defaultOptions = useState ( ( ) => ( ( ) => ( { } ) ) ( ) ) [ 0 ] ;
49- const props : Omit < PdfViewerProps , 'src' | 'scale' | 'rotation' | 'workerSrc' | 'renderAllPages' | 'textLayer' | 'password' | 'options' > & { src : unknown ; scale : number ; rotation : number ; workerSrc : string ; renderAllPages : boolean ; textLayer : boolean ; password : unknown ; options : Record < string , any > } = {
43+ const props : Omit < PdfViewerProps , 'src' | 'scale' | 'rotation' | 'workerSrc' | 'standardFontDataUrl' | ' renderAllPages' | 'textLayer' | 'password' | 'options' > & { src : unknown ; scale : number ; rotation : number ; workerSrc : string ; standardFontDataUrl : string ; renderAllPages : boolean ; textLayer : boolean ; password : unknown ; options : Record < string , any > } = {
5044 ..._props ,
5145 src : _props . src ?? undefined ,
5246 scale : _props . scale ?? 1 ,
5347 rotation : _props . rotation ?? 0 ,
5448 workerSrc : _props . workerSrc ?? 'https://cdn.jsdelivr.net/npm/pdfjs-dist@6.0.227/build/pdf.worker.min.mjs' ,
49+ standardFontDataUrl : _props . standardFontDataUrl ?? 'https://cdn.jsdelivr.net/npm/pdfjs-dist@6.0.227/standard_fonts/' ,
5550 renderAllPages : _props . renderAllPages ?? false ,
5651 textLayer : _props . textLayer ?? true ,
5752 password : _props . password ?? undefined ,
5853 options : _props . options ?? __defaultOptions ,
5954 } ;
6055 const attrs : Record < string , unknown > = ( ( ) => {
61- const { src, page, scale, rotation, workerSrc, renderAllPages, textLayer, password, options, defaultValue, onPageChange, defaultPage, ...rest } = _props as PdfViewerProps & Record < string , unknown > ;
62- void src ; void page ; void scale ; void rotation ; void workerSrc ; void renderAllPages ; void textLayer ; void password ; void options ; void defaultValue ; void onPageChange ; void defaultPage ;
56+ const { src, page, scale, rotation, workerSrc, standardFontDataUrl , renderAllPages, textLayer, password, options, defaultValue, onPageChange, defaultPage, ...rest } = _props as PdfViewerProps & Record < string , unknown > ;
57+ void src ; void page ; void scale ; void rotation ; void workerSrc ; void standardFontDataUrl ; void renderAllPages ; void textLayer ; void password ; void options ; void defaultValue ; void onPageChange ; void defaultPage ;
6358 return rest ;
6459 } ) ( ) ;
60+ const cancelled = useRef ( false ) ;
6561 const containerEl = useRef < any > ( null ) ;
62+ const pdfjsLib = useRef < any > ( null ) ;
6663 const renderToken = useRef ( 0 ) ;
6764 const observer = useRef < any > ( null ) ;
6865 const loadingTask = useRef < any > ( null ) ;
@@ -118,6 +115,7 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
118115 cfg . data = src ;
119116 }
120117 if ( props . password != null ) cfg . password = props . password ;
118+ if ( props . standardFontDataUrl ) cfg . standardFontDataUrl = props . standardFontDataUrl ;
121119 return cfg ;
122120 }
123121 async function renderPage ( pdf : any , pageNum : any , container : any ) {
@@ -150,7 +148,7 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
150148 const tl = document . createElement ( 'div' ) ;
151149 tl . className = 'textLayer' ;
152150 pageDiv . appendChild ( tl ) ;
153- const layer = new pdfjsLib . TextLayer ( {
151+ const layer = new pdfjsLib . current . TextLayer ( {
154152 textContentSource : page . streamTextContent ( ) ,
155153 container : tl ,
156154 viewport
@@ -223,6 +221,7 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
223221 }
224222 const { onError : _rozieProp_onError , onLoad : _rozieProp_onLoad , onPasswordrequest : _rozieProp_onPasswordrequest } = props ;
225223 const load = useCallback ( async ( ) => {
224+ if ( ! pdfjsLib . current ) return ;
226225 const token = ++ renderToken . current ;
227226 if ( observer . current ) {
228227 observer . current . disconnect ( ) ;
@@ -238,7 +237,7 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
238237 if ( containerEl . current ) containerEl . current . innerHTML = '' ;
239238 if ( ! props . src ) return ;
240239 try {
241- loadingTask . current = pdfjsLib . getDocument ( buildSource ( ) ) ;
240+ loadingTask . current = pdfjsLib . current . getDocument ( buildSource ( ) ) ;
242241 loadingTask . current . onPassword = ( _updatePassword : any , reason : any ) => {
243242 _rozieProp_onPasswordrequest && _rozieProp_onPasswordrequest ( {
244243 reason
@@ -314,13 +313,21 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
314313 }
315314
316315 useEffect ( ( ) => {
317- pdfjsLib . GlobalWorkerOptions . workerSrc = _workerSrcRef . current ;
316+ cancelled . current = false ;
318317 containerEl . current = viewerEl . current ;
319318 setCurrent ( Math . max ( 1 , _pageRef . current ) ) ;
320319 setZoom ( _scaleRef . current ) ;
321320 setRot ( _rotationRef . current ) ;
322- load ( ) ;
321+ // lazy-load the engine (SSR-safe + code-split), then configure the worker and
322+ // load the document.
323+ import ( 'pdfjs-dist' ) . then ( ( mod : any ) => {
324+ if ( cancelled . current ) return ;
325+ pdfjsLib . current = mod ;
326+ pdfjsLib . current . GlobalWorkerOptions . workerSrc = _workerSrcRef . current ;
327+ load ( ) ;
328+ } ) ;
323329 return ( ) => {
330+ cancelled . current = true ;
324331 renderToken . current ++ ;
325332 if ( observer . current ) {
326333 observer . current . disconnect ( ) ;
@@ -344,7 +351,7 @@ const PdfViewer = forwardRef<PdfViewerHandle, PdfViewerProps>(function PdfViewer
344351 useEffect ( ( ) => {
345352 if ( _watch2First . current ) { _watch2First . current = false ; return ; }
346353 const v = props . workerSrc ;
347- if ( v ) pdfjsLib . GlobalWorkerOptions . workerSrc = v ;
354+ if ( pdfjsLib . current && v ) pdfjsLib . current . GlobalWorkerOptions . workerSrc = v ;
348355 } , [ props . workerSrc ] ) ;
349356 useEffect ( ( ) => {
350357 if ( _watch3First . current ) { _watch3First . current = false ; return ; }
0 commit comments