
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
react-ipdf-viewer
Advanced tools
A lightweight, dependency-free media viewer for PDFs and other media types with advanced controls
A React component for viewing PDF documents using Mozilla's pdf.js
. Embed a customizable PDF viewer in your React applications with page navigation, zoom, rotation, download, print, and full-screen support.
Install the package via npm:
npm install react-ipdf-viewer
Or using Yarn:
yarn add react-ipdf-viewer
Ensure you have the following peer dependencies installed:
react
(>=16.8.0)react-dom
(>=16.8.0)pdfjs-dist
(>=2.9.359)Install them if needed:
npm install react react-dom pdfjs-dist
Import the ReactIPdfViewer
component and provide a PDF file URL or path via the src
prop. Customize with props like theme
, showControls
, and feature toggles. The component supports PDF documents only; place local PDFs in your public/
folder or use a hosted URL.
import React from 'react';
import ReactIPdfViewer from 'react-ipdf-viewer';
import './App.css';
const PDF = 'sample.pdf';
const App = () => {
return (
<div className="App">
<ReactIPdfViewer
src={PDF}
theme="dark"
autoHeight
showControls
allowDownload
allowPrint
allowRotate
allowFullScreen
/>
</div>
);
};
export default App;
import React from 'react';
import ReactIPdfViewer from 'react-ipdf-viewer';
import './App.css';
const sampleImage = 'sample.jpg';
const App = () => {
return (
<div className="App">
<ReactIPdfViewer
src={sampleImage}
theme="dark"
autoHeight
showControls
allowDownload
allowPrint
allowRotate
allowFullScreen
/>
</div>
);
};
export default App;
import React from 'react';
import ReactIPdfViewer from 'react-ipdf-viewer';
const App = () => {
return (
<ReactIPdfViewer
src="https://example.com/sample.pdf"
theme="light"
autoHeight={false}
showControls={false}
allowDownload={true}
allowPrint={false}
allowRotate={true}
allowFullScreen={true}
/>
);
};
export default App;
ReactIPdfViewer
A React component to render a PDF viewer with customizable controls.
Prop | Type | Description | Default | Example |
---|---|---|---|---|
src | string | URL or path to the PDF file to render. Also accepts document={{ url }} . | None | 'sample.pdf' or 'https://example.com/sample.pdf' |
theme | string | Sets the viewer’s theme. Options: 'light' , 'dark' . | 'light' | 'dark' |
autoHeight | boolean | Automatically adjusts the viewer’s height to fit the PDF or container. | false | true |
showControls | boolean | Displays the toolbar with navigation and feature controls. | true | true |
allowDownload | boolean | Enables a download button in the toolbar. | true | true |
allowPrint | boolean | Enables a print button in the toolbar. | true | false |
allowRotate | boolean | Enables rotation controls (left, right) in the toolbar. | true | true |
allowFullScreen | boolean | Enables a full-screen toggle button in the toolbar. | true | true |
<ReactIPdfViewer
src="sample.pdf"
theme="dark"
autoHeight
showControls
allowDownload
allowPrint={false}
allowRotate
allowFullScreen
/>
The ReactIPdfViewer
component may encounter issues when loading or rendering PDFs. Below are common errors and solutions.
Invalid PDF URL
src
prop points to an invalid or inaccessible URL.public/
or a hosted HTTPS URL.<ReactIPdfViewer src="/sample.pdf" />
CORS Restrictions
public/
.const App = () => {
const pdfUrl = process.env.NODE_ENV === 'development'
? '/sample.pdf'
: 'https://your-domain.com/sample.pdf';
return <ReactIPdfViewer src={pdfUrl} />;
};
Content Security Policy (CSP) Error
pdf.js
are blocked by your app’s CSP.connect-src
directive.
<meta http-equiv="Content-Security-Policy" content="connect-src 'self' blob:;">
Mobile Rendering Issues
pdf.js
canvas rendering. Test with a local PDF.<ReactIPdfViewer src="/sample.pdf" autoHeight />
File Loading Failure
pdf.js
.import React, { useState } from 'react';
import ReactIPdfViewer from 'react-ipdf-viewer';
const App = () => {
const [error, setError] = useState(null);
const handleError = (err) => setError(err.message);
return (
<div>
{error ? <p>Error: {error}</p> : null}
<ReactIPdfViewer
src="sample.pdf"
onError={handleError}
theme="dark"
showControls
/>
</div>
);
};
src
is a valid string. Use a fallback URL if needed.
const pdfUrl = src || '/fallback.pdf';
<ReactIPdfViewer src={pdfUrl} />;
onError
(if supported) or wrap in try-catch.pdf.js
warnings (e.g., missing CMaps).Beyond the core props, ReactIPdfViewer
offers:
css
prop (e.g., { navbarWrapper: 'custom-navbar' }
).Contributions are welcome! To contribute:
git checkout -b feature/YourFeature
).git commit -m 'Add YourFeature'
).git push origin feature/YourFeature
).Please ensure your code follows the existing style and includes tests where applicable.
This project is licensed under the MIT License. See the LICENSE file for details.
For issues, questions, or feature requests, please open an issue on the GitHub repository or contact Sudeepa R.# react-ipdf-viewer
FAQs
A lightweight, dependency-free media viewer for PDFs and other media types with advanced controls
The npm package react-ipdf-viewer receives a total of 0 weekly downloads. As such, react-ipdf-viewer popularity was classified as not popular.
We found that react-ipdf-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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.