react-doc-viewer, react-file-viewer, and react-pdf are npm packages designed to help developers embed document viewing capabilities directly into React applications. While all three aim to simplify rendering documents like PDFs, Word files, or spreadsheets, they differ significantly in scope, underlying technology, and maintenance status. react-pdf focuses exclusively on PDF rendering using Mozilla's PDF.js, offering fine-grained control and active development. In contrast, react-file-viewer and react-doc-viewer attempt broader file-type support but vary in reliability and current viability.
When you need to show documents inside a React app—whether it’s a PDF invoice, a Word report, or a PowerPoint deck—you’ll quickly realize browsers don’t natively support most office formats. That’s where libraries like react-doc-viewer, react-file-viewer, and react-pdf come in. But they solve the problem in very different ways, with major implications for security, maintainability, and user experience.
Before diving into features, check the health of the project:
react-file-viewer is deprecated. Its npm page and GitHub README clearly state: “This library is no longer maintained.” It hasn’t seen meaningful updates since 2019 and uses legacy React patterns (createClass, PropTypes from prop-types without modern context). Do not use it in new projects.
react-doc-viewer and react-pdf are both actively maintained as of 2024, with recent releases and responsive issue tracking.
This alone eliminates react-file-viewer from serious consideration unless you’re maintaining old code.
react-pdf: PDF Only, But Done Rightreact-pdf focuses exclusively on PDFs using Mozilla’s battle-tested PDF.js. It renders entirely in the browser—no iframes, no external services.
// react-pdf: Client-side PDF rendering
import { Document, Page } from 'react-pdf';
function PDFViewer({ fileUrl }) {
return (
<Document file={fileUrl}>
<Page pageNumber={1} />
<Page pageNumber={2} />
</Document>
);
}
You get full control: lazy-load pages, handle loading states, customize scale, and even extract text.
react-doc-viewer: Multi-Format via Third-Party Embedsreact-doc-viewer supports PDF, DOCX, XLSX, PPTX, TXT, and images—but not all client-side. For non-PDF files, it falls back to embedding Google Docs Viewer or Microsoft Office Online in an <iframe>.
// react-doc-viewer: Unified API, mixed rendering strategies
import DocViewer, { DocViewerRenderers } from 'react-doc-viewer';
const docs = [{ uri: "/report.docx", fileType: "docx" }];
function DocumentViewer() {
return (
<DocViewer
documents={docs}
pluginRenderers={DocViewerRenderers}
/>
);
}
For PDFs, it uses react-pdf under the hood. For .docx, it loads https://docs.google.com/gview?url=...&embedded=true in an iframe. This means:
react-file-viewer: Deprecated Multi-Format AttemptHistorically, react-file-viewer tried to render multiple formats using various client-side parsers (e.g., mammoth for DOCX, pdfjs-dist for PDFs). But due to lack of maintenance, many dependencies are outdated and insecure.
// react-file-viewer (deprecated — do not use)
import FileViewer from 'react-file-viewer';
// This may break in modern React apps
<FileViewer fileType="docx" filePath="/report.docx" />
It never handled complex formats reliably, and today it’s incompatible with React 18+ in strict mode.
react-pdf: Safe for Sensitive DataSince everything runs in-browser, you can serve PDFs from private, authenticated endpoints. No data leaves your user’s machine.
// Works with private URLs (e.g., behind auth)
<Document file={{ url: '/api/documents/secret.pdf', httpHeaders: { Authorization: 'Bearer ...' } }} />
react-doc-viewer: Public URLs Required for Non-PDFGoogle Docs Viewer cannot access private or localhost URLs. Your .docx file must be publicly reachable so Google’s servers can fetch it. This is a dealbreaker for internal apps or confidential documents.
// ❌ Fails if /report.docx requires authentication
const docs = [{ uri: "/report.docx", fileType: "docx" }]; // Will show blank or error in iframe
Workarounds involve pre-converting files to PDF on the backend—which defeats the purpose of using a multi-format viewer.
react-file-viewer: Unmaintained = RiskyOutdated dependencies (like old versions of pdfjs-dist) may contain unpatched vulnerabilities. Not suitable for production.
react-pdf: Full Control Over UIYou build the viewer UI yourself—pagination, zoom buttons, thumbnails. This gives pixel-perfect control but requires more work.
// Example: Custom zoom control
import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
function CustomPDFViewer({ file }) {
const [numPages, setNumPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
function onDocumentLoadSuccess({ numPages }) {
setNumPages(numPages);
}
return (
<div>
<Document file={file} onLoadSuccess={onDocumentLoadSuccess}>
<Page pageNumber={pageNumber} />
</Document>
<p>Page {pageNumber} of {numPages}</p>
</div>
);
}
react-doc-viewer: Opinionated, Limited CustomizationIt provides a built-in toolbar and layout, but customization options are minimal. You can’t easily change colors, add annotations, or override the iframe behavior for non-PDF files.
react-file-viewer: No Meaningful Customization TodayEven if it worked, its theming system is based on legacy CSS modules and doesn’t integrate well with modern styling solutions like Tailwind or CSS-in-JS.
react-pdf: Explicit and ReliableOffers callbacks for loading, success, and error states per page or document.
<Document
file={file}
loading={<div>Loading PDF...</div>}
error={<div>Failed to load PDF</div>}
noData={<div>No PDF provided</div>}
/>
react-doc-viewer: Basic Error HandlingProvides an onError callback, but iframe-based renderers often fail silently (e.g., Google Docs returns a blank page instead of an error).
<DocViewer
documents={docs}
onError={(error) => console.error('DocViewer error:', error)}
/>
react-file-viewer: Unreliable Error ReportingError props exist but often don’t fire due to unhandled promise rejections in outdated dependencies.
| Scenario | Recommended Package |
|---|---|
| Only PDFs, client-side, secure environment | react-pdf |
| Multiple formats, public files, quick prototype | react-doc-viewer |
| Legacy app already using it | Keep react-file-viewer temporarily, but migrate ASAP |
| Need offline DOCX rendering | None of these — convert to PDF on backend first |
react-pdf is the clear winner—secure, customizable, and actively maintained.react-doc-viewer offers convenience at the cost of third-party dependency.react-file-viewer—its deprecation notice exists for good reason.If your app handles sensitive documents, invest in server-side conversion to PDF and use react-pdf. It’s more work upfront but pays off in security, reliability, and long-term maintainability.
Choose react-doc-viewer if you need a modern, actively maintained solution that supports multiple file types (including PDF, DOCX, XLSX, PPTX, and images) through a unified API and relies on external services like Google Docs Viewer or Microsoft Office Online for non-PDF formats. It’s suitable for internal tools or dashboards where you can accept third-party rendering dependencies and don’t require offline or fully client-side processing.
Avoid react-file-viewer in new projects — it is officially deprecated as of its latest npm release, with the README explicitly stating it is no longer maintained. The package depends on outdated libraries and lacks support for modern React features like hooks or concurrent rendering. If you encounter it in legacy code, plan a migration to react-doc-viewer or react-pdf depending on your file-type needs.
Choose react-pdf if your primary requirement is robust, client-side PDF rendering without external dependencies. It provides full control over pagination, text selection, zoom, and loading states using PDF.js under the hood. Ideal for applications requiring offline PDF support, custom UI controls, accessibility compliance, or integration into secure environments where third-party iframes are prohibited.
| Extension | MIME Type | Available |
|---|---|---|
| bmp | image/bmp | ✓ |
| doc | application/msword | ✓ |
| docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document | ✓ |
| htm | text/htm | ✓ |
| html | text/html | ✓ |
| jpg | image/jpg | ✓ |
| jpeg | image/jpeg | ✓ |
| application/pdf | ✓ | |
| png | image/png | ✓ |
| ppt | application/vnd.ms-powerpoint | ✓ |
| pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation | ✓ |
| tiff | image/tiff | ✓ |
| txt | text/plain | ✓ |
| xls | application/vnd.ms-excel | ✓ |
| xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | ✓ |
npm i react-doc-viewer
# or
yarn add react-doc-viewer
Warning - By default the component height will expand and contract to the current loaded file. The width will expand to fill the parent.
DocViewer requires at least an array of document objects to function. Each document object must have a uri to a file, either a url that returns a file or a local file.
import DocViewer from "react-doc-viewer";
function App() {
const docs = [
{ uri: "https://url-to-my-pdf.pdf" },
{ uri: require("./example-files/pdf.pdf") }, // Local File
];
return <DocViewer documents={docs} />;
}
To use the included renderers.
DocViewerRenderers is an Array of all the included renderers.
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";
<DocViewer
pluginRenderers={DocViewerRenderers}
{/* ... */}
/>;
Or you can import individual renderers.
import DocViewer, { PDFRenderer, PNGRenderer } from "react-doc-viewer";
<DocViewer
pluginRenderers={[PDFRenderer, PNGRenderer]}
{/* ... */}
/>;
To create a custom renderer, that will just exist for your project.
import React from "react";
import DocViewer from "react-doc-viewer";
const MyCustomPNGRenderer: DocRenderer = ({
mainState: { currentDocument },
}) => {
if (!currentDocument) return null;
return (
<div id="my-png-renderer">
<img id="png-img" src={currentDocument.fileData as string} />
</div>
);
};
MyCustomPNGRenderer.fileTypes = ["png", "image/png"];
MyCustomPNGRenderer.weight = 1;
And supply it to DocViewer > pluginRenderers inside an Array.
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";
<DocViewer
pluginRenderers={[MyCustomPNGRenderer]}
documents={
[
// ...
]
}
/>;
If you need to prevent the actual loading of the file by react-doc-viewer.
you can decorate your custom renderer with a callback to do as you wish. e.g. Load the file yourself in an iFrame.
MyCustomPNGRenderer.fileLoader = ({
documentURI,
signal,
fileLoaderComplete,
}) => {
myCustomFileLoaderCode().then(() => {
// Whenever you have finished you must call fileLoaderComplete() to remove the loading animation
fileLoaderComplete();
});
};
You can provide a theme object with one or all of the available properties.
<DocViewer
documents={docs}
theme={{
primary: "#5296d8",
secondary: "#ffffff",
tertiary: "#5296d899",
text_primary: "#ffffff",
text_secondary: "#5296d8",
text_tertiary: "#00000099",
disableThemeScrollbar: false,
}}
/>
Any styling applied to the <DocViewer> component, is directly applied to the main div container.
<DocViewer documents={docs} className="my-doc-viewer-style" />
Each component / div already has a DOM id that can be used to style any part of the document viewer.
#react-doc-viewer #header-bar {
background-color: #faf;
}
<DocViewer documents={docs} style={{width: 500, height: 500}} />
import styled from "styled-components";
//...
<MyDocViewer documents={docs} />;
//...
const MyDocViewer = styled(DocViewer)`
border-radius: 10px;
`;
You can provide a config object, which configures parts of the component as required.
<DocViewer documents={docs} config={{
header: {
disableHeader: false,
disableFileName: false,
retainURLParams: false
}
}} />
Step 1 - Create a new folder inside src/plugins.
e.g.
src/plugins/jpg
Inside this folder, create a Renderer React Typescript file.
e.g.
index.tsx
Step 2 - Inside JPGRenderer, export a functional component of type DocRenderer
import React from "react";
import { DocRenderer } from "../../types";
// Be sure that Renderer correctly uses type DocRenderer
const JPGRenderer: DocRenderer = ({ mainState: { currentDocument } }) => {
if (!currentDocument) return null;
return (
<div id="jpg-renderer">
<img id="jpg-img" src={currentDocument.fileData as string} />
</div>
);
};
export default JPGRenderer;
// List the MIME types that this renderer will respond to
JPGRenderer.fileTypes = ["jpg", "jpeg", "image/jpg", "image/jpeg"];
// If you have more than one renderer for the same MIME type, use weight. higher is more preferable.
// Included renderers have a weight of zero
JPGRenderer.weight = 1;
If you are creating a new renderer, also update src/plugins/index.ts with an import to your new renderer file, and Export it as part of the DocViewerRenderers Array.
// ...
import JPGRenderer from "./jpg";
export const DocViewerRenderers = [
// ...
JPGRenderer,
];
You can pass a callback function to config.header.overrideComponent that returns a React Element. The function's parameters will be populated and usable, this function will also be re-called whenever the mainState updates.
Parameters include the state object from the main component, and document navigation functions for previousDocument and nextDocument.
Example:
const myHeader: IHeaderOverride = (state, previousDocument, nextDocument) => {
if (!state.currentDocument || state.config?.header?.disableFileName) {
return null;
}
return (
<>
<div>{state.currentDocument.uri || ""}</div>
<div>
<button
onClick={previousDocument}
disabled={state.currentFileNo === 0}
>
Previous Document
</button>
<button
onClick={nextDocument}
disabled={state.currentFileNo >= state.documents.length - 1}
>
Next Document
</button>
</div>
</>
);
};
<DocViewer
pluginRenderers={DocViewerRenderers}
documents={
{
/**/
}
}
config={{
header: {
overrideComponent: myHeader;
},
},
}
/>
DocViewer props| name | type |
|---|---|
| documents | IDocument[] |
| className? | string |
| style? | React.CSSProperties |
| config? | IConfig |
| theme? | ITheme |
| pluginRenderers? | DocRenderer[] |
IDocument| name | type |
|---|---|
| uri | string |
| fileType? | string |
| fileData? | `string |
IConfig| name | type |
|---|---|
| header? | IHeaderConfig |
IHeaderConfig| name | type |
|---|---|
| disableHeader? | boolean |
| disableFileName? | boolean |
| retainURLParams? | boolean |
| overrideComponent? | IHeaderOverride |
IHeaderOverride () => ReactElement<any, any> | null| name | type |
|---|---|
| state | IMainState |
| previousDocument | () => void |
| nextDocument | () => void |
returns | `ReactElement<any, any> |
ITheme| name | type |
|---|---|
| primary? | string |
| secondary? | string |
| tertiary? | string |
| text_primary? | string |
| text_secondary? | string |
| text_tertiary? | string |
| disableThemeScrollbar? | boolean |
DocRenderer extends React.FC<DocRendererProps>| name | type |
|---|---|
| fileTypes | string[] |
| weight | number |
| fileLoader? | FileLoaderFunction ` |
FileLoaderFunction(props: FileLoaderFuncProps) => void
FileLoaderFuncProps| name | type |
|---|---|
| documentURI | string |
| signal | AbortSignal |
| fileLoaderComplete | FileLoaderComplete |
FileLoaderComplete| name | type |
|---|---|
| fileReader | FileReader |
DocRendererProps| name | type |
|---|---|
| mainState | IMainState |
IMainState| name | type |
|---|---|
| currentFileNo | number |
| documents | IDocument[] |
| documentLoading? | boolean |
| currentDocument? | IDocument |
| rendererRect? | DOMRect |
| config? | IConfig |