React Fast PDF Viewer

1.0.33 · active · verified Tue Apr 21

react-fast-pdf is a performant React component library engineered for efficient viewing and previewing of PDF documents within web applications. It is currently at stable version 1.0.33 and maintains an active, continuous release cadence, with new versions automatically published to npm upon merging pull requests to the main branch. Key differentiators for this library include its highly optimized rendering engine, allowing it to handle PDF files of virtually any size with robust performance, and its design for easy customization to integrate seamlessly into various application designs. Developed and supported by Expensify, it aims to consolidate the best features found in other PDF viewing solutions into a single, streamlined, and user-friendly tool for React developers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to embed and render a PDF document using the `PdfViewer` component, including basic state management for the PDF URL and event handling for load success or error. It provides a full, runnable React component.

import React, { useState, useEffect } from 'react';
import { PdfViewer } from 'react-fast-pdf';

const MyPdfComponent: React.FC = () => {
  const [pdfUrl, setPdfUrl] = useState<string | null>(null);

  useEffect(() => {
    // In a real application, you might fetch this URL or get it from props
    // For demonstration, using a public sample PDF URL
    setPdfUrl('https://www.africau.edu/images/default/sample.pdf');
  }, []);

  if (!pdfUrl) {
    return <div>Loading PDF...</div>;
  }

  return (
    <div style={{ border: '1px solid #ccc', borderRadius: '8px', overflow: 'hidden', maxWidth: '900px', margin: '20px auto' }}>
      <h2 style={{ padding: '10px 20px', margin: 0, backgroundColor: '#f0f0f0', borderBottom: '1px solid #eee' }}>Document Viewer</h2>
      <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '70vh' }}>
        <PdfViewer
          url={pdfUrl}
          width={800} // Example fixed width
          height={600} // Example fixed height
          onLoadSuccess={() => console.log('PDF loaded successfully!')}
          onLoadError={(error) => console.error('Error loading PDF:', error)}
        />
      </div>
      <p style={{ padding: '10px 20px', margin: 0, fontSize: '0.9em', color: '#555' }}>
        This example demonstrates basic usage of `react-fast-pdf` to display a PDF document.
        The viewer automatically handles rendering and optimizing performance for large files.
      </p>
    </div>
  );
};

export default MyPdfComponent;

view raw JSON →