Convert Figma logo to code with AI

react-pdf-viewer logoreact-pdf-viewer

A React component to view a PDF document

2,400
271
2,400
356

Top Related Projects

50,177

PDF Reader in JavaScript

Quick Overview

React PDF Viewer is a powerful React component for viewing PDF documents in web applications. It provides a feature-rich, customizable interface for rendering and interacting with PDF files, supporting various viewing modes, annotations, and search functionality.

Pros

  • High performance and smooth rendering of PDF documents
  • Extensive customization options and plugin system
  • Responsive design, suitable for both desktop and mobile devices
  • Comprehensive documentation and examples

Cons

  • Relatively large bundle size due to its feature-rich nature
  • Learning curve for advanced customizations and plugin development
  • Limited browser support for older versions (IE11 not supported)
  • Requires a commercial license for use in non-open-source projects

Code Examples

  1. Basic usage:
import { Viewer } from '@react-pdf-viewer/core';
import '@react-pdf-viewer/core/lib/styles/index.css';

const App = () => (
  <div style={{ height: '750px' }}>
    <Viewer fileUrl="/path/to/document.pdf" />
  </div>
);
  1. Using plugins:
import { Viewer } from '@react-pdf-viewer/core';
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';

import '@react-pdf-viewer/core/lib/styles/index.css';
import '@react-pdf-viewer/default-layout/lib/styles/index.css';

const defaultLayoutPluginInstance = defaultLayoutPlugin();

const App = () => (
  <div style={{ height: '750px' }}>
    <Viewer
      fileUrl="/path/to/document.pdf"
      plugins={[defaultLayoutPluginInstance]}
    />
  </div>
);
  1. Custom toolbar:
import { Viewer, ToolbarSlot } from '@react-pdf-viewer/core';
import { toolbarPlugin } from '@react-pdf-viewer/toolbar';

const toolbarPluginInstance = toolbarPlugin();
const { Toolbar } = toolbarPluginInstance;

const App = () => (
  <div style={{ height: '750px' }}>
    <div style={{ height: '50px' }}>
      <Toolbar>
        {(slots: ToolbarSlot) => {
          const { ZoomIn, ZoomOut } = slots;
          return (
            <>
              <ZoomOut />
              <ZoomIn />
            </>
          );
        }}
      </Toolbar>
    </div>
    <div style={{ height: 'calc(100% - 50px)' }}>
      <Viewer
        fileUrl="/path/to/document.pdf"
        plugins={[toolbarPluginInstance]}
      />
    </div>
  </div>
);

Getting Started

  1. Install the package:
npm install @react-pdf-viewer/core @react-pdf-viewer/default-layout
  1. Import and use the Viewer component:
import { Viewer } from '@react-pdf-viewer/core';
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';

import '@react-pdf-viewer/core/lib/styles/index.css';
import '@react-pdf-viewer/default-layout/lib/styles/index.css';

const defaultLayoutPluginInstance = defaultLayoutPlugin();

const App = () => (
  <div style={{ height: '750px' }}>
    <Viewer
      fileUrl="/path/to/document.pdf"
      plugins={[defaultLayoutPluginInstance]}
    />
  </div>
);

export default App;

Competitor Comparisons

50,177

PDF Reader in JavaScript

Pros of pdf.js

  • Widely adopted and battle-tested, with a large community and extensive browser support
  • Offers a standalone viewer with a full-featured UI out of the box
  • Provides lower-level APIs for custom implementations and advanced use cases

Cons of pdf.js

  • Requires more setup and configuration for React integration
  • Less optimized for React-specific features and patterns
  • May have a steeper learning curve for React developers

Code Comparison

pdf.js (vanilla JavaScript):

pdfjsLib.getDocument('document.pdf').promise.then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport({ scale: scale });
    // Render page
  });
});

react-pdf-viewer (React component):

import { Viewer } from '@react-pdf-viewer/core';

const MyPdfViewer = () => (
  <Viewer fileUrl="/path/to/document.pdf" />
);

react-pdf-viewer is specifically designed for React applications, offering a more straightforward integration with React components and hooks. It provides a higher-level abstraction, making it easier to implement PDF viewing functionality in React projects. However, pdf.js offers more flexibility and control over the PDF rendering process, making it suitable for a wider range of applications and use cases beyond React.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React PDF viewer

A React component to view a PDF document. It's written in TypeScript, and powered by React hooks completely.

React PDF viewer

// Core viewer
import { Viewer } from '@react-pdf-viewer/core';

// Plugins
import { defaultLayoutPlugin } from '@react-pdf-viewer/default-layout';

// Import styles
import '@react-pdf-viewer/core/lib/styles/index.css';
import '@react-pdf-viewer/default-layout/lib/styles/index.css';

// Create new plugin instance
const defaultLayoutPluginInstance = defaultLayoutPlugin();

<Viewer
    fileUrl='/assets/pdf-open-parameters.pdf'
    plugins={[
        // Register plugins
        defaultLayoutPluginInstance,
        ...
    ]}
/>

Features

Basic features

  • Support password protected document
  • Zooming: Support custom levels such as actual size, page fit, and page width
  • Navigation between pages
  • Can go to the first and last pages quickly
  • Search for text
  • Preview page thumbnails
  • View and navigate the table of contents
  • List and download attachments
  • Rotating
  • Text selection and hand tool modes
  • Different scrolling modes
  • Full screen mode
  • Can open a file from local. Users can drag and drop a local file to view it
  • Download file
  • View the document properties
  • Support SSR
  • Print
  • Theming
  • Dark mode
  • Accessibility

Customization

  • The toolbar can be customized easily
  • All text items can be localized in another language

License

You have to purchase a Commercial License at the official website.

About

This project is developed by Nguyen Huu Phuoc. I love building products and sharing knowledge.