React Fast PDF Viewer
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
-
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This usually indicates that `react-fast-pdf` (or React itself) is being loaded from multiple conflicting copies, or it's used in a project with an incompatible React version (e.g., React 17 with a React 18-dependent library).fixCheck your `package.json` for conflicting React versions. Ensure `react` and `react-dom` are both `^18.0.0`. Run `npm dedupe` or `yarn why react` to identify and resolve duplicate React installations. -
Failed to load PDF document. Network error or invalid file format.
cause The URL provided to the `PdfViewer` component is either inaccessible (e.g., CORS issues, network down), points to a non-existent file, or the file at the URL is not a valid PDF document.fixVerify the PDF `url` prop is correct and publicly accessible. Check browser console for network errors (e.g., 404, CORS blocking). Ensure the hosted file is indeed a valid PDF. -
TypeError: Cannot read properties of undefined (reading 'x')
cause This error often occurs when an internal dependency, such as `lodash`, is missing or an incompatible version is installed, leading to `react-fast-pdf` trying to access undefined properties of expected utility functions.fixEnsure `lodash@^4` is correctly installed as a direct or transitive dependency in your project. Run `npm install lodash@^4` if it's missing or check `npm ls lodash` to confirm the installed version.
Warnings
- gotcha When developing locally, the package's README suggests modifying `package.json` to point to a local source directory (`"react-fast-pdf": "../src"`). This change is critical for local testing but must be reverted before committing or deploying, as it will break production builds and installations.
- breaking The library explicitly lists `react@18.x` and `react-dom@18.x` as peer dependencies. Using `react-fast-pdf` with older React versions (e.g., React 17 or earlier) may lead to runtime errors, inconsistent behavior, or invalid hook calls.
- gotcha A peer dependency on `lodash@4.x` is required. If `lodash` is not installed or an incompatible version is present in your project, `react-fast-pdf` may encounter runtime errors due to missing utility functions.
Install
-
npm install react-fast-pdf -
yarn add react-fast-pdf -
pnpm add react-fast-pdf
Imports
- PdfViewer
const PdfViewer = require('react-fast-pdf');import { PdfViewer } from 'react-fast-pdf'; - PdfViewerProps
import type { PdfViewerProps } from 'react-fast-pdf'; - usePdf
import { usePdf } from 'react-fast-pdf';
Quickstart
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;