From 3aeec4d23ee47a306932c78f16751ee7a84abf9f Mon Sep 17 00:00:00 2001 From: Ahmed Al-Taiar Date: Thu, 1 May 2025 20:46:50 -0400 Subject: [PATCH] Fix PDF once and for all --- web/package.json | 3 +- web/src/components/PDF/PDF.tsx | 51 ++++--- yarn.lock | 257 ++++++++++++++++++++++++++++++++- 3 files changed, 285 insertions(+), 26 deletions(-) diff --git a/web/package.json b/web/package.json index e353037..f67583f 100755 --- a/web/package.json +++ b/web/package.json @@ -41,7 +41,8 @@ "react": "18.3.1", "react-colorful": "^5.6.1", "react-dom": "18.3.1", - "react-html-parser": "^2.0.2" + "react-html-parser": "^2.0.2", + "react-pdf": "^9.2.1" }, "devDependencies": { "@redwoodjs/vite": "8.4.0", diff --git a/web/src/components/PDF/PDF.tsx b/web/src/components/PDF/PDF.tsx index ebb55eb..9f2647f 100755 --- a/web/src/components/PDF/PDF.tsx +++ b/web/src/components/PDF/PDF.tsx @@ -1,7 +1,14 @@ +import { mdiOpenInNew } from '@mdi/js'; +import Icon from '@mdi/react'; import { useState } from 'react' +import { Document, Page as PdfPage, pdfjs } from "react-pdf"; +import "react-pdf/dist/Page/AnnotationLayer.css"; +import "react-pdf/dist/Page/TextLayer.css"; -import { mdiAlertOutline } from '@mdi/js' -import Icon from '@mdi/react' +pdfjs.GlobalWorkerOptions.workerSrc = new URL( + "pdfjs-dist/build/pdf.worker.min.mjs", + import.meta.url +).toString(); interface PDFProps { url: string @@ -9,29 +16,33 @@ interface PDFProps { } const PDF = ({ url, form = false }: PDFProps) => { - const [error, setError] = useState(false) + const [numPages, setNumPages] = useState(0); + function onLoadSuccess({ numPages }: { numPages: number }) { + setNumPages(numPages); + } - return error ? ( -
- - - Could not load PDF, this is common in in-app browsers, try opening this - page in a regular browser - -
- ) : ( -