New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

pdfjs-react-viewer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdfjs-react-viewer

A React component library for viewing PDFs using PDF.js

latest
Source
npmnpm
Version
0.1.0-18
Version published
Maintainers
1
Created
Source

PDF.js Viewer React

npm version npm downloads License: MIT

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.

✨ Features

  • 📄 Render PDFs directly in your React application
  • 🔍 Customizable zoom/scale options
  • 📱 Responsive design
  • 🎨 Customizable navigation controls
  • 🚀 Compatible with React 18+ and Vite projects
  • 📦 Lightweight with minimal dependencies

🔥 Live Demo

Check out our live demo to see the component in action!

📦 Installation

npm install pdfjs-react-viewer
# or
yarn add pdfjs-react-viewer

🔧 Compatibility

This library has been tested and works well with:

  • ✅ React 18+
  • ✅ Vite 4.x and 6.x projects
  • ✅ Next.js 13+
  • ✅ Create React App
  • ✅ TypeScript projects

🤔 Why Use This Library?

  • Simplified Integration: No need to deal with the complexities of PDF.js directly
  • React-First Design: Built specifically for React applications
  • Lightweight: Minimal impact on your bundle size
  • TypeScript Support: Full TypeScript definitions included
  • Customizable: Easily style and extend to match your application's design
  • Actively Maintained: Regular updates and improvements

💻 Basic Usage

import { PDFJSViewer } from 'pdfjs-react-viewer';

function App() {
  return (
    <div className="app">
      <PDFJSViewer 
        pdfUrl="https://example.com/sample.pdf" 
        scale={1.5}
      />
    </div>
  );
}

With Custom Scale Control

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>
  );
}

🚀 Getting Started with Vite

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;

📚 API Reference

PDFJSViewer

The main component for rendering PDFs.

import { PDFJSViewer } from 'pdfjs-react-viewer';

Props

PropTypeDefaultDescription
pdfUrlstringRequiredURL of the PDF to display
scalenumber1.5Scale factor for rendering the PDF
renderControlsfunctionDefault controlsCustom renderer for navigation controls
classNamestring''CSS class name for the container
workerSrcstringMozilla CDNWorker source URL
onPageChangefunction-Callback fired when the page changes
onDocumentLoadfunction-Callback fired when the PDF document is loaded
viewerRefReact.RefObject<PDFJSViewerAPI>-Ref to access the viewer API methods

PDFControls

Navigation controls component that can be used separately or customized.

import { PDFControls } from 'pdfjs-react-viewer';

Props

PropTypeDescription
currentPagenumberCurrent page number
totalPagesnumberTotal number of pages
onPrevPagefunctionGo to previous page
onNextPagefunctionGo to next page
isPrevDisabledbooleanWhether the previous button is disabled
isNextDisabledbooleanWhether the next button is disabled

Creating Custom Controls

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.

Example of Custom Controls

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>
  );
}

Using Callbacks and the Viewer API

Page Change Tracking

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>
  );
}

Document Load Events

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>
  );
}

Programmatic Control with Viewer API

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>
  );
}

Types

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';

Development

Building the package

yarn build:package

Building the demo

yarn build:demo

Packing the package for installation

yarn pack

License

MIT

Keywords

pdf

FAQs

Package last updated on 06 May 2025

Did you know?

Socket

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.

Install

Related posts