百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-doc-viewer

> 编程语言
开源

反应-doc-查看器内容 - 当前可渲染的文件类型 - 安装 - 核心 - 用法 - 基础 - 包含渲染器 - 自定义渲染器 - Th

372 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

反应-doc-查看器内容 - 当前可渲染的文件类型 - 安装 - 核心 - 用法 - 基础 - 包含渲染器 - 自定义渲染器 - Th

react-doc-viewer

Contents

  • Current Renderable File Types
  • Installation
    • Core
  • Usage
    • Basic
    • Included Renderers
    • Custom Renderer
    • Themed
    • Styling
      • CSS Class
      • CSS Class Default Override
      • React Inline
      • StyledComponent
    • Config
  • Contributing
    • Creating a Renderer Plugin
  • Overriding Header Component
  • API

Current Renderable File Types

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 ✓
pdf 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 ✓

Installation

Core

bash
 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.

typescript
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 ;
}

Included Renderers

To use the included renderers. DocViewerRenderers is an Array of all the included renderers.

typescript
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";

;

Or you can import individual renderers.

typescript
import DocViewer, { PDFRenderer, PNGRenderer } from "react-doc-viewer";

;

Custom Renderer

To create a custom renderer, that will just exist for your project.

typescript
import React from "react";
import DocViewer from "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 DocViewer > pluginRenderers inside an Array.

typescript
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";

;

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.

typescript
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.

xml

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

- StyledComponent

typescript
import styled from "styled-components";
//...
;
//...
const MyDocViewer = styled(DocViewer)`
  border-radius: 10px;
`;

Config

You can provide a config object, which configures parts of the component as required.

xml

Contributing

Creating a Renderer Plugin

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

…

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.

typescript
// ...
import JPGRenderer from "./jpg";

export const DocViewerRenderers = [
  // ...
  JPGRenderer,
];

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:

typescript

const myHeader: IHeaderOverride = (state, previousDocument, nextDocument) => {
    if (!state.currentDocument || state.config?.header?.disableFileName) {
      return null;
    }

    return (
      <>
        

        

      
    );
  };

API


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 | null

name type
state IMainState
previousDocument () => void
nextDocument () => void
returns `ReactElement

ITheme

name type
primary? string
secondary? string
tertiary? string
text_primary? string
text_secondary? string
text_tertiary? string
disableThemeScrollbar? boolean

DocRenderer extends React.FC\

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言