2025-09-09 14:51:23 +02:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2025-11-13 11:38:23 +01:00
|
|
|
|
2026-04-03 18:57:39 +02:00
|
|
|
import { Document, Page, pdfjs } from "react-pdf";
|
2025-09-09 14:51:23 +02:00
|
|
|
import { LoadingPage } from "./loading";
|
|
|
|
|
|
2026-04-03 18:57:39 +02:00
|
|
|
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
|
2025-09-09 14:51:23 +02:00
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
cMapUrl: "/cmaps/",
|
|
|
|
|
standardFontDataUrl: "/standard_fonts/",
|
|
|
|
|
wasmUrl: "/wasm/",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const PDFViewer = ({ url }: { url: string }) => {
|
|
|
|
|
const [numPages, setNumPages] = useState<number>();
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const [width, setWidth] = useState(0);
|
|
|
|
|
const file = useMemo(() => ({ url }), [url]);
|
|
|
|
|
const onResize = useCallback<ResizeObserverCallback>((entries) => {
|
|
|
|
|
const [entry] = entries;
|
|
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
|
setWidth(entry.contentRect.width);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const observer = new ResizeObserver(onResize);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (containerRef.current) {
|
|
|
|
|
observer.observe(containerRef.current);
|
|
|
|
|
setWidth(containerRef.current.offsetWidth);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-10-10 16:18:43 +02:00
|
|
|
className="mx-1 flex w-full justify-center overflow-clip rounded-md shadow-lg outline"
|
2025-09-09 14:51:23 +02:00
|
|
|
ref={containerRef}
|
|
|
|
|
>
|
|
|
|
|
<Document
|
2025-10-10 16:18:43 +02:00
|
|
|
className={"aspect-[1/1.4142] overflow-auto"}
|
2025-09-09 14:51:23 +02:00
|
|
|
file={file}
|
|
|
|
|
loading={<LoadingPage />}
|
2025-09-09 16:18:10 +02:00
|
|
|
noData={
|
|
|
|
|
"Errore nel caricare il documento, riprova più tardi o Scaricalo"
|
|
|
|
|
}
|
2026-04-03 18:57:39 +02:00
|
|
|
onLoadSuccess={({ numPages }) => setNumPages(numPages)}
|
2025-09-09 14:51:23 +02:00
|
|
|
options={options}
|
|
|
|
|
>
|
|
|
|
|
{Array.from(new Array(numPages), (_el, index) => (
|
|
|
|
|
<Page
|
2026-03-08 01:02:57 +01:00
|
|
|
key={`page_${
|
|
|
|
|
// biome-ignore lint/suspicious/noArrayIndexKey: <ok>
|
|
|
|
|
index + 1
|
|
|
|
|
}`}
|
2025-09-09 14:51:23 +02:00
|
|
|
pageNumber={index + 1}
|
2026-04-03 18:57:39 +02:00
|
|
|
renderAnnotationLayer={false}
|
|
|
|
|
renderTextLayer={false}
|
2025-09-09 14:51:23 +02:00
|
|
|
width={width}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Document>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|