You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've a document view in my application which should be used to show A4 PDF files.
Its a rule only to upload A4 pdf files, but the shape of the document view suggest it.
But all other shapes should be supported too.
To get the correct scale factor for the pdf I did following. Otherwise the PDF was scaled to big and did not fit on a page:
functionPDFArea({ fileUrl, height, width }: PDFAreaProps){constcontainerRef=useRef<HTMLDivElement>(null);const[numPages,setNumPages]=useState<number>();const[containerSize,setContainerSize]=useState({width: 0,height: 0});const[pageDimensions,setPageDimensions]=useState<PDFPageDimensions>({width: 0,height: 0,});const[scale,setScale]=useState<number|null>(null);functiononDocumentLoadSuccess(pdf: pdfjs.PDFDocumentProxy): void{setNumPages(pdf.numPages);// Get the dimensions of the first page (assuming all pages has the same shape)pdf.getPage(1).then(page=>{constviewport=page.getViewport({scale: 1});setPageDimensions({width: viewport.width,height: viewport.height,});});}useEffect(()=>{functionupdateContainerSize(){if(containerRef.current){setContainerSize({width: containerRef.current.offsetWidth,height: containerRef.current.offsetHeight,});console.log(containerRef.current.offsetWidth);}}// Initial measurementupdateContainerSize();// Update on window resizewindow.addEventListener("resize",updateContainerSize);// Cleanup event listenerreturn()=>window.removeEventListener("resize",updateContainerSize);},[]);// Calculate scale factor when dimensions changeuseEffect(()=>{if(pageDimensions&&containerSize.width>0&&containerSize.height>0){constscaleX=containerSize.width/pageDimensions.width;constscaleY=containerSize.height/pageDimensions.height;// Choose the smaller scale to fit the page within the containerconstnewScale=Math.min(scaleX,scaleY);setScale(newScale);}},[pageDimensions,containerSize]);return(<divref={containerRef}style={{width: width,height: height,overflowY: "auto",}}className="overflow-y-auto bg-gray-100 flex justify-center py-4"><divclassName="w-max"><Documentloading={<Logo/>}file={fileUrl}onLoadSuccess={onDocumentLoadSuccess}options={options}>{numPages&&(<>{scale===null ? (// Display a loading spinner or placeholder<Logo/>) : (// Render the PDF pagesArray.from(newArray(numPages),(_,index)=>(<divkey={`page_${index+1}`}className="mb-8 last:mb-0 bg-white shadow-md rounded"><Pagekey={`page_${index+1}`}pageNumber={index +1}scale={scale*0.95}className="mx-auto"renderMode="canvas"/></div>)))}</>)}</Document></div></div>);}
Now following is happening if I visit the page:
The Logo is shown because of loading={<Logo />}
Immediatly after that the pages are shown but they are empty:
The pdf is successfully loaded
I have a problem with step 2 and I would like to hide the whole thing until it is fully loaded. In the time between I would like to see .
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello folks,
I've a document view in my application which should be used to show A4 PDF files.
Its a rule only to upload A4 pdf files, but the shape of the document view suggest it.
But all other shapes should be supported too.
To get the correct scale factor for the pdf I did following. Otherwise the PDF was scaled to big and did not fit on a page:
Now following is happening if I visit the page:
The Logo is shown because of
loading={<Logo />}
Immediatly after that the pages are shown but they are empty:
I have a problem with step 2 and I would like to hide the whole thing until it is fully loaded. In the time between I would like to see .
Can you help me archiving this?
T
Beta Was this translation helpful? Give feedback.
All reactions