
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
pdfjs-react-viewer
Advanced tools
A lightweight, customizable PDF viewer component for React applications. Built on top of Mozilla's PDF.js, this library makes it easy to integrate PDF viewing capabilities into your React projects.
Check out our live demo to see the component in action!
npm install pdfjs-react-viewer
# or
yarn add pdfjs-react-viewer
This library has been tested and works well with:
import { PDFJSViewer } from 'pdfjs-react-viewer';
function App() {
return (
<div className="app">
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
scale={1.5}
/>
</div>
);
}
import { useState } from 'react';
import { PDFJSViewer } from 'pdfjs-react-viewer';
function App() {
const [scale, setScale] = useState(1.5);
return (
<div className="app">
<div className="scale-controls">
<button onClick={() => setScale(prev => Math.max(0.5, prev - 0.25))}>Zoom Out</button>
<span>{Math.round(scale * 100)}%</span>
<button onClick={() => setScale(prev => prev + 0.25)}>Zoom In</button>
</div>
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
scale={scale}
/>
</div>
);
}
Quickly set up a new Vite project with PDF.js Viewer React:
# Create a new Vite project
npm create vite@latest my-pdf-app --template react-ts
cd my-pdf-app
# Install dependencies
npm install
npm install pdfjs-react-viewer
# Start the development server
npm run dev
Then edit your src/App.tsx to include the PDF viewer:
import { PDFJSViewer } from 'pdfjs-react-viewer';
import './App.css';
function App() {
return (
<div className="container">
<h1>PDF Viewer Example</h1>
<div className="pdf-container">
<PDFJSViewer
pdfUrl="https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf"
scale={1.5}
/>
</div>
</div>
);
}
export default App;
The main component for rendering PDFs.
import { PDFJSViewer } from 'pdfjs-react-viewer';
| Prop | Type | Default | Description |
|---|---|---|---|
pdfUrl | string | Required | URL of the PDF to display |
scale | number | 1.5 | Scale factor for rendering the PDF |
renderControls | function | Default controls | Custom renderer for navigation controls |
className | string | '' | CSS class name for the container |
workerSrc | string | Mozilla CDN | Worker source URL |
onPageChange | function | - | Callback fired when the page changes |
onDocumentLoad | function | - | Callback fired when the PDF document is loaded |
viewerRef | React.RefObject<PDFJSViewerAPI> | - | Ref to access the viewer API methods |
Navigation controls component that can be used separately or customized.
import { PDFControls } from 'pdfjs-react-viewer';
| Prop | Type | Description |
|---|---|---|
currentPage | number | Current page number |
totalPages | number | Total number of pages |
onPrevPage | function | Go to previous page |
onNextPage | function | Go to next page |
isPrevDisabled | boolean | Whether the previous button is disabled |
isNextDisabled | boolean | Whether the next button is disabled |
You can create custom navigation controls by providing a function to the renderControls prop of the PDFJSViewer component. The function receives the same props as the PDFControls component.
import { PDFJSViewer, PDFControlsProps } from 'pdfjs-react-viewer';
// Custom controls component
const CustomControls = (props: PDFControlsProps) => {
const { currentPage, totalPages, onPrevPage, onNextPage, isPrevDisabled, isNextDisabled } = props;
return (
<div className="custom-controls">
<button
onClick={onPrevPage}
disabled={isPrevDisabled}
className="custom-button"
>
Previous
</button>
<span className="page-indicator">
Page {currentPage} of {totalPages}
</span>
<button
onClick={onNextPage}
disabled={isNextDisabled}
className="custom-button"
>
Next
</button>
</div>
);
};
// Using the custom controls
function App() {
return (
<div className="app">
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
scale={1.5}
renderControls={CustomControls}
/>
</div>
);
}
You can track page changes using the onPageChange callback:
import { PDFJSViewer } from 'pdfjs-react-viewer';
function App() {
const handlePageChange = (pageNumber, totalPages) => {
console.log(`Viewing page ${pageNumber} of ${totalPages}`);
// Update UI or state based on page change
};
return (
<div className="app">
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
onPageChange={handlePageChange}
/>
</div>
);
}
You can respond to document loading using the onDocumentLoad callback:
import { PDFJSViewer } from 'pdfjs-react-viewer';
function App() {
const handleDocumentLoad = (totalPages) => {
console.log(`PDF loaded with ${totalPages} pages`);
// Initialize UI or state based on document load
};
return (
<div className="app">
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
onDocumentLoad={handleDocumentLoad}
/>
</div>
);
}
You can programmatically control the viewer using the viewerRef:
import { useRef } from 'react';
import { PDFJSViewer, PDFJSViewerAPI } from 'pdfjs-react-viewer';
function App() {
// Create a ref to access the viewer API
const pdfViewerRef = useRef<PDFJSViewerAPI>(null);
// Function to jump to a specific page
const goToPage5 = () => {
if (pdfViewerRef.current) {
pdfViewerRef.current.goToPage(5);
}
};
// Function to get the current page
const logCurrentPage = () => {
if (pdfViewerRef.current) {
const currentPage = pdfViewerRef.current.getCurrentPage();
console.log(`Currently on page ${currentPage}`);
}
};
return (
<div className="app">
<div className="controls">
<button onClick={goToPage5}>Go to Page 5</button>
<button onClick={logCurrentPage}>Log Current Page</button>
</div>
<PDFJSViewer
pdfUrl="https://example.com/sample.pdf"
viewerRef={pdfViewerRef}
/>
</div>
);
}
The library exports TypeScript types for all components and PDF.js interfaces:
import type {
PDFJSViewerProps,
PDFControlsProps,
PDFDocumentProxy,
PDFPageProxy,
PDFViewport,
PDFRenderContext,
PDFRenderTask,
PDFJSLib,
PDFJSViewerAPI // New API interface
} from 'pdfjs-react-viewer';
yarn build:package
yarn build:demo
yarn pack
MIT
FAQs
A React component library for viewing PDFs using PDF.js
We found that pdfjs-react-viewer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.