» npm install react-pdf
reactjs - How to use PDF.JS with React? - Stack Overflow
reactjs - how to use pdfjs worker in react and typescript? - Stack Overflow
How to Use Mozilla PDF.js with React.js for Client-Side Rendering & Basic Tools?
PDF viewer in Next.js
https://www.npmjs.com/package/react-pdf ?
More on reddit.comVideos
Hey r/reactjs,
I wanted to share an article I just wrote about a topic that can be surprisingly tricky: rendering PDFs in React.
It's easy enough to get a static image of a PDF page onto a <canvas>, but if you've ever tried to make the text selectable or have links that actually work, you know the real challenge begins there.
I ran into this and did a deep dive into how PDF.js actually works. It turns out the magic is in its layer system. My article breaks down the three key layers:
The Canvas Layer: The base visual representation of the PDF.
The Text Layer: A transparent layer of HTML elements positioned perfectly over the canvas, making the text selectable and searchable.
The Annotation Layer: Another transparent layer that handles things like clickable links within the PDF.
The post walks through what each layer does and then provides a step-by-step guide on how to build a React component that stacks these layers correctly to create a fully interactive and accessible PDF viewer.
Hope this is useful for anyone who's had to wrestle with PDFs in their projects! I'll be hanging around in the comments to answer any questions.
Article Link: Understanding PDF.js Layers and How to Use Them in ReactJS
» npm install @react-pdf-viewer/core
» npm install react-pdf-js
This worked for me:
Import the package as below
import * as pdfjsLib from "pdfjs-dist";
Copy the pdf.worker.min.mjs file from node_modules/pdfjs-dist/build/ directory into your public folder
Set the worker to point to the file in your public folder
pdfjsLib.GlobalWorkerOptions.workerSrc = window.location.origin + "/pdf.worker.min.mjs";
Helpful articles:
https://medium.com/@hesseclaus/using-pdfjs-with-react-app-rewired-f1f3a2527c45
https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples
I'm also running pdfjs v4 in vite and found this works:
import { GlobalWorkerOptions } from 'pdfjs-dist/legacy/build/pdf.mjs';
GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/legacy/build/pdf.worker.min.mjs', import.meta.url).toString();
(We're running a legacy build, but I assume the main build works the same way)
Vite won't recognise it's a js module and hence won't minify it, so I'm using the min.mjs version here.
» npm install @react-pdf/renderer