npm i react-doc-viewer
# or
yarn add react-doc-viewer
Usage
Warning - By default the component height will expand and contract to the current loaded file. The width will expand to fill the parent.
Basic
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} />;
}
Included Renderers
To use the included renderers.
DocViewerRenderers is an Array of all the included renderers.
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();
});
};
Themed
You can provide a theme object with one or all of the available properties.
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.
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.
react-doc-viewer
Contents
Current Renderable File Types
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
✓
Installation
Core
Usage
Basic
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.
Included Renderers
To use the included renderers.
DocViewerRenderers
is an Array of all the included renderers.Or you can import individual renderers.
Custom Renderer
To create a custom renderer, that will just exist for your project.
And supply it to DocViewer > pluginRenderers inside an
Array
.Custom File Loader
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.Themed
You can provide a theme object with one or all of the available properties.
Styling
Any styling applied to the
<DocViewer>
component, is directly applied to the maindiv
container.- CSS Class
- CSS Class Default Override
Each component / div already has a DOM id that can be used to style any part of the document viewer.
- React Inline
- StyledComponent
Config
You can provide a config object, which configures parts of the component as required.
Contributing
Creating a Renderer Plugin
Step 1 - Create a new folder inside
src/plugins
.Inside this folder, create a Renderer React Typescript file.
Step 2 - Inside JPGRenderer, export a functional component of type
DocRenderer
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 DocViewerRenderersArray
.Overriding Header Component
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 forpreviousDocument
andnextDocument
.Example:
API
DocViewer props
IDocument[]
string
React.CSSProperties
IConfig
ITheme
DocRenderer[]
IDocument
string
string
IConfig
IHeaderConfig
IHeaderConfig
boolean
boolean
boolean
IHeaderOverride
IHeaderOverride
() =>ReactElement<any, any> | null
IMainState
() => void
() => void
returns
ITheme
string
string
string
string
string
string
boolean
DocRenderer
extends React.FC<DocRendererProps
>string[]
number
FileLoaderFunction
`FileLoaderFunction
(props:
FileLoaderFuncProps
) => voidFileLoaderFuncProps
string
AbortSignal
FileLoaderComplete
FileLoaderComplete
FileReader
DocRendererProps
IMainState
IMainState
IDocument[]
IDocument
IConfig