» npm install react-pdf
Videos
» npm install react-pdf-js
Hey everyone,
I hope you're all doing well! 🖐️
I'm currently working on a React.js project that involves rendering PDFs on the client side, and I stumbled upon Mozilla's PDF.js library. While it seems promising, I'm facing some challenges when it comes to integrating it into my project and adding some basic tools like annotations and highlighting.However, I've been struggling to find comprehensive documentation on how to integrate PDF.js with React.js effectively.
Has anyone here successfully used PDF.js with React.js before? I'd really appreciate some guidance on the best practices for integration and any tips or resources to add basic tools for annotations and highlighting.
Feel free to share your experiences, code snippets, or any helpful tutorials that you've come across. Your insights will be of great help to a fellow developer.
Thank you all in advance! 😊🙏
I spent too much time today, piecing together fragments of other answers to this question. So here is a complete answer.
First you install pdfjs-dist:
npm install pdfjs-dist
And here is how to use it in an actual viewer component:
import React, { useEffect, useState, useRef, useCallback } from 'react';
import pdfjsLib from "pdfjs-dist/build/pdf";
import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry";
export default function PdfViewer({url}){
const canvasRef = useRef();
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;
const [pdfRef, setPdfRef] = useState();
const [currentPage, setCurrentPage] = useState(1);
const renderPage = useCallback((pageNum, pdf=pdfRef) => {
pdf && pdf.getPage(pageNum).then(function(page) {
const viewport = page.getViewport({scale: 1.5});
const canvas = canvasRef.current;
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: canvas.getContext('2d'),
viewport: viewport
};
page.render(renderContext);
});
}, [pdfRef]);
useEffect(() => {
renderPage(currentPage, pdfRef);
}, [pdfRef, currentPage, renderPage]);
useEffect(() => {
const loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(loadedPdf => {
setPdfRef(loadedPdf);
}, function (reason) {
console.error(reason);
});
}, [url]);
const nextPage = () => pdfRef && currentPage < pdfRef.numPages && setCurrentPage(currentPage + 1);
const prevPage = () => currentPage > 1 && setCurrentPage(currentPage - 1);
return <canvas ref={canvasRef}></canvas>;
}
This import will clear the undefined issue:
import * as pdfjsLib from "pdfjs-dist/build/pdf";
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 @pdf-viewer/react