Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cyntler/react-doc-viewer
File viewer for React.
https://github.com/cyntler/react-doc-viewer
doc-viewer file-viewer react-library reactjs
Last synced: 3 days ago
JSON representation
File viewer for React.
- Host: GitHub
- URL: https://github.com/cyntler/react-doc-viewer
- Owner: cyntler
- License: apache-2.0
- Created: 2022-04-14T11:34:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T11:49:30.000Z (19 days ago)
- Last Synced: 2024-10-30T06:53:29.497Z (5 days ago)
- Topics: doc-viewer, file-viewer, react-library, reactjs
- Language: TypeScript
- Homepage: https://cyntler.github.io/react-doc-viewer
- Size: 10.9 MB
- Stars: 383
- Watchers: 4
- Forks: 126
- Open Issues: 102
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome-list - react-doc-viewer
README
[![npm-version](https://img.shields.io/npm/v/@cyntler/react-doc-viewer.svg)](https://www.npmjs.com/package/@cyntler/react-doc-viewer)
[![npm-download](https://img.shields.io/npm/dt/@cyntler/react-doc-viewer.svg)](https://www.npmjs.com/package/@cyntler/react-doc-viewer)# @cyntler/react-doc-viewer
File viewer for **React v17+**.
> This is a fork of https://github.com/Alcumus/react-doc-viewer (inactivity for a long time).
## Important note!
> [!WARNING]
> This library uses the official MS Office online document viewing service. This means it works on an iframe basis and only supports public file URLs! Therefore, it may not be compatible with all projects. Currently, there is no way to natively render MS Office documents in the browser.## Table of Contents
- [Supported file types](#supported-file-types)
- [Storybook Demo](#storybook-demo)
- [Installation](#installation)
- [Usage](#usage)
- [Required styles](#required-styles)
- [Basic](#basic)
- [Initial Active Document](#initial-active-document)
- [Control over the displayed document](#control-over-the-displayed-document)
- [Displaying blob/uploaded documents](#displaying-blobuploaded-documents)
- [Included Renderers](#included-renderers)
- [Custom Renderer](#custom-renderer)
- [Custom File Loader](#custom-file-loader)
- [Theme](#theme)
- [Custom pre-fetch HTTP Verb](#custom-pre-fetch-http-verb)
- [Custom Request Headers](#custom-request-headers)
- [Internationalization (i18n)](#internationalization-i18n)
- [Styling](#styling)
- [CSS Class](#css-class)
- [CSS Class Default Override](#css-class-default-override)
- [React Inline](#react-inline)
- [Styled Components](#styled-components)
- [Using DocViewerRef](#using-docviewerref)
- [Config](#config)
- [Overriding Header Component](#overriding-header-component)
- [Overriding Loading Renderer](#overriding-loading-renderer)
- [Overriding No Renderer (Error)](#overriding-no-renderer-error)## Supported file types
| Extension | MIME Type | Comments |
| --------- | ------------------------------------------------------------------------- | ------------- |
| bmp | image/bmp | |
| csv | text/csv | |
| odt | application/vnd.oasis.opendocument.text | |
| doc | application/msword | Public URLs only! |
| docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document | Public URLs only! |
| gif | image/gif | |
| htm | text/htm | |
| html | text/html | |
| jpg | image/jpg | |
| jpeg | image/jpeg | |
| pdf | application/pdf | |
| png | image/png | |
| ppt | application/vnd.ms-powerpoint | Public URLs only! |
| pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation | Public URLs only! |
| tiff | image/tiff | |
| txt | text/plain | |
| xls | application/vnd.ms-excel | Public URLs only! |
| xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | Public URLs only! |
| mp4 | video/mp4 | |
| webp | image/webp | |## Storybook Demo
https://cyntler.github.io/react-doc-viewer
## Installation
Use one of the package managers for Node.js.
```bash
npm i @cyntler/react-doc-viewer
# or
yarn add @cyntler/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._
### Required styles
The library exports a CSS file containing classes needed for correct rendering of e.g. PDF files. It is best to include it at the beginning of the application or in the place where you use this library.
```tsx
import "@cyntler/react-doc-viewer/dist/index.css";
```### 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.```tsx
import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";function App() {
const docs = [
{ uri: "https://url-to-my-pdf.pdf" }, // Remote file
{ uri: require("./example-files/pdf.pdf") }, // Local File
];return ;
}
```### Initial Active Document
By default, the first item in your `documents` array will be displayed after the component is rendered. However, there is a prop `initialActiveDocument` that you can point to the initial document that should be displayed.
```tsx
import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";const App = () => {
const docs = [
{ uri: "https://url-to-my-pdf.pdf" }, // Remote file
{ uri: require("./example-files/pdf.pdf") }, // Local File
];return (
);
};
```### Control over the displayed document
From version **1.11.0** you can control the displayed document through two props: `activeDocument` and `onDocumentChange`.
```jsx
const DocViewerControlOverDisplayedDocument = () => {
const docs = [
{ uri: "https://url-to-my-pdf.pdf" }, // Remote file
{ uri: require("./example-files/pdf.pdf") }, // Local File
];
const [activeDocument, setActiveDocument] = useState(docs[0]);const handleDocumentChange = (document) => {
setActiveDocument(document);
};return (
<>
>
);
};
```### Displaying blob/uploaded documents
Since **v1.6.2** you can use documents in the form of blobs, which allows you to e.g. display uploaded files.
```jsx
const DocViewerWithInputApp = () => {
const [selectedDocs, setSelectedDocs] = useState([]);return (
<>
el.target.files?.length &&
setSelectedDocs(Array.from(el.target.files))
}
/>
({
uri: window.URL.createObjectURL(file),
fileName: file.name,
}))}
pluginRenderers={DocViewerRenderers}
/>
>
);
};
```### Included Renderers
To use the included renderers.
`DocViewerRenderers` is an Array of all the included renderers.```tsx
import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";;
```Or you can import individual renderers.
```tsx
import DocViewer, { PDFRenderer, PNGRenderer } from "@cyntler/react-doc-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";;
```### Custom Renderer
To create a custom renderer, that will just exist for your project.
```tsx
import React from "react";
import DocViewer from "@cyntler/react-doc-viewer";const MyCustomPNGRenderer: DocRenderer = ({
mainState: { currentDocument },
}) => {
if (!currentDocument) return null;return (
);
};MyCustomPNGRenderer.fileTypes = ["png", "image/png"];
MyCustomPNGRenderer.weight = 1;
```And supply it to `pluginRenderers` inside an `Array`.
```tsx
import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";;
```### Custom File Loader
If you need to prevent the actual loading of the file by `@cyntler/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.```tsx
MyCustomPNGRenderer.fileLoader = ({
documentURI,
signal,
fileLoaderComplete,
}) => {
myCustomFileLoaderCode().then(() => {
// Whenever you have finished you must call fileLoaderComplete() to remove the loading animation
fileLoaderComplete();
});
};
```## Theme
You can provide a theme object with one or all of the available properties.
```xml
```
## Custom pre-fetch HTTP Verb
Some services (such as AWS) provide URLs that works only for one pre-configured verb.
By default, `@cyntler/react-doc-viewer` fetches document metadata through a `HEAD` request in order to guess its `Content-Type`.
If you need to have a specific verb for the pre-fetching, use the `prefetchMethod` option on the DocViewer:```tsx
import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";;
```## Custom Request Headers
Provide request headers, i.e. for authenticating with an API etc.
```tsx
const headers = {
"X-Access-Token": "1234567890",
"My-Custom-Header": "my-custom-value",
};;
```## Internationalization (i18n)
From **v1.6.0** you can pass the `language` prop to the `DocViewer` component to get translated sentences and words that can be displayed by this library.
```xml
```
The translations are based on the `.json` files that can be found in the `src/locales` directory.
## Styling
Any styling applied to the `` component, is directly applied to the main `div` container.
### CSS Class
```xml
```
### CSS Class Default Override
Each component / div already has a DOM id that can be used to style any part of the document viewer.
```css
#react-doc-viewer #header-bar {
background-color: #faf;
}
```### React Inline
```xml
```
### Styled Components
```tsx
import styled from "styled-components";// ...
;
// ...
const MyDocViewer = styled(DocViewer)`
border-radius: 10px;
`;
```## Using DocViewerRef
Since **v1.13.0** you can control the display of the document with `reference`.
```tsx
import DocViewer, { DocViewerRef } from "@cyntler/react-doc-viewer";export const UsingRef = () => {
const docViewerRef = useRef(null);return (
<>
docViewerRef?.current?.prev()}>
Prev Document By Ref
docViewerRef?.current?.next()}>
Next Document By Ref
>
);
};
```## Config
You can provide a config object, which configures parts of the component as required.
```tsx
```
### 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 for `previousDocument` and `nextDocument`.Example:
```tsx
const MyHeader: IHeaderOverride = (state, previousDocument, nextDocument) => {
if (!state.currentDocument || state.config?.header?.disableFileName) {
return null;
}return (
<>
{state.currentDocument.uri || ""}
Previous Document
= state.documents.length - 1}
>
Next Document
>
);
};;
```### Overriding Loading Renderer
You can pass a callback function to `config.loadingRenderer.overrideComponent` that returns a React Element.
Example:
```tsx
const MyLoadingRenderer = ({ document, fileName }) => {
const fileText = fileName || document?.fileType || "";if (fileText) {
returnLoading Renderer ({fileText})...;
}return
Loading Renderer...;
};;
```By default, the loading component is rendered if document loading process takes more than 500 ms.
You can change this time value or disable this feature to make the component display immediately:
```tsx
const MyLoadingRenderer = ({ document, fileName }) => {
...
};;
```### Overriding No Renderer (Error)
You can pass a callback function to `config.noRenderer.overrideComponent` that returns a React Element.
Example:
```tsx
const MyNoRenderer = ({ document, fileName }) => {
const fileText = fileName || document?.fileType || "";if (fileText) {
returnNo Renderer Error! ({fileText});
}return
No Renderer Error!;
};;
```