Coder Social home page Coder Social logo

cyntler / react-doc-viewer Goto Github PK

View Code? Open in Web Editor NEW
313.0 3.0 111.0 7.77 MB

File viewer for React.

Home Page: https://cyntler.github.io/react-doc-viewer

License: Apache License 2.0

TypeScript 98.23% JavaScript 0.89% PostScript 0.88%
file-viewer reactjs doc-viewer react-library

react-doc-viewer's Introduction

npm-version npm-download

@cyntler/react-doc-viewer

File viewer for React.

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.

Supported file types

Extension MIME Type
bmp image/bmp
csv text/csv
odt application/vnd.oasis.opendocument.text
doc application/msword
docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
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
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
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.

 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.

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, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

function App() {
  const docs = [
    { uri: "https://url-to-my-pdf.pdf" }, // Remote file
    { uri: require("./example-files/pdf.pdf") }, // Local File
  ];

  return <DocViewer documents={docs} pluginRenderers={DocViewerRenderers} />;
}

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.

import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

const App = () => {
  const docs = [
    { uri: "https://url-to-my-pdf.pdf" }, // Remote file
    { uri: require("./example-files/pdf.pdf") }, // Local File
  ];

  return (
    <DocViewer
      documents={docs}
      initialActiveDocument={docs[1]}
      pluginRenderers={DocViewerRenderers}
    />
  );
};

Control over the displayed document

From version 1.11.0 you can control the displayed document through two props: activeDocument and onDocumentChange.

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 (
    <>
      <DocViewer
        documents={docs}
        activeDocument={activeDocument}
        onDocumentChange={handleDocumentChange}
      />
    </>
  );
};

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.

const DocViewerWithInputApp = () => {
  const [selectedDocs, setSelectedDocs] = useState<File[]>([]);

  return (
    <>
      <input
        type="file"
        accept=".pdf"
        multiple
        onChange={(el) =>
          el.target.files?.length &&
          setSelectedDocs(Array.from(el.target.files))
        }
      />
      <DocViewer
        documents={selectedDocs.map((file) => ({
          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.

import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

<DocViewer
  pluginRenderers={DocViewerRenderers}
  {/* ... */}
/>;

Or you can import individual renderers.

import DocViewer, { PDFRenderer, PNGRenderer } from "@cyntler/react-doc-viewer";

<DocViewer
  pluginRenderers={[PDFRenderer, PNGRenderer]}
  {/* ... */}
/>;

Custom Renderer

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

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

import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

<DocViewer
  pluginRenderers={[MyCustomPNGRenderer]}
  documents={
    [
      // ...
    ]
  }
/>;

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.

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.

<DocViewer
  documents={docs}
  theme={{
    primary: "#5296d8",
    secondary: "#ffffff",
    tertiary: "#5296d899",
    textPrimary: "#ffffff",
    textSecondary: "#5296d8",
    textTertiary: "#00000099",
    disableThemeScrollbar: false,
  }}
/>

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:

import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

<DocViewer prefetchMethod="GET" />;

Custom Request Headers

Provide request headers, i.e. for authenticating with an API etc.

const headers = {
  "X-Access-Token": "1234567890",
  "My-Custom-Header": "my-custom-value",
};

<DocViewer documents={docs} prefetchMethod="GET" requestHeaders={headers} />;

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.

<DocViewer documents={docs} language="pl" />

The translations are based on the .json files that can be found in the src/locales directory.

Styling

Any styling applied to the <DocViewer> component, is directly applied to the main div container.

CSS Class

<DocViewer documents={docs} className="my-doc-viewer-style" />

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-doc-viewer #header-bar {
  background-color: #faf;
}

React Inline

<DocViewer documents={docs} style={{ width: 500, height: 500 }} />

Styled Components

import styled from "styled-components";

// ...

<MyDocViewer documents={docs} />;

// ...

const MyDocViewer = styled(DocViewer)`
  border-radius: 10px;
`;

Using DocViewerRef

Since v1.13.0 you can control the display of the document with reference.

import DocViewer, { DocViewerRef } from "@cyntler/react-doc-viewer";

export const UsingRef = () => {
  const docViewerRef = useRef<DocViewerRef>(null);

  return (
    <>
      <div>
        <button onClick={() => docViewerRef?.current?.prev()}>
          Prev Document By Ref
        </button>
        <button onClick={() => docViewerRef?.current?.next()}>
          Next Document By Ref
        </button>
      </div>
      <DocViewer
        ref={docViewerRef}
        documents={docs}
        config={{ header: { disableHeader: true } }}
      />
    </>
  );
};

Config

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,
    },
    csvDelimiter: ",", // "," as default,
    pdfZoom: {
      defaultZoom: 1.1, // 1 as default,
      zoomJump: 0.2, // 0.1 as default,
    },
    pdfVerticalScrollByDefault: true, // false as default
  }}
/>

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:

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,
    },
  }}
/>;

Overriding Loading Renderer

You can pass a callback function to config.loadingRenderer.overrideComponent that returns a React Element.

Example:

const MyLoadingRenderer = ({ document, fileName }) => {
  const fileText = fileName || document?.fileType || "";

  if (fileText) {
    return <div>Loading Renderer ({fileText})...</div>;
  }

  return <div>Loading Renderer...</div>;
};

<DocViewer
  pluginRenderers={DocViewerRenderers}
  documents={
    {
      // ...
    }
  }
  config={{
    loadingRenderer: {
      overrideComponent: MyLoadingRenderer,
    },
  }}
/>;

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:

const MyLoadingRenderer = ({ document, fileName }) => {
  ...
};

<DocViewer
  pluginRenderers={DocViewerRenderers}
  documents={
    {
      // ...
    }
  }
  config={{
    loadingRenderer: {
      overrideComponent: MyLoadingRenderer,
      showLoadingTimeout: false, // false if you want to disable or number to provide your own value (ms)
    },
  }}
/>;

Overriding No Renderer (Error)

You can pass a callback function to config.noRenderer.overrideComponent that returns a React Element.

Example:

const MyNoRenderer = ({ document, fileName }) => {
  const fileText = fileName || document?.fileType || "";

  if (fileText) {
    return <div>No Renderer Error! ({fileText})</div>;
  }

  return <div>No Renderer Error!</div>;
};

<DocViewer
  pluginRenderers={DocViewerRenderers}
  documents={
    {
      // ...
    }
  }
  config={{
    noRenderer: {
      overrideComponent: MyNoRenderer,
    },
  }}
/>;

react-doc-viewer's People

Contributors

aaquibnaved avatar alantoa avatar andykorek avatar ayush1096 avatar bdhamithkumara avatar connortjolley avatar cyntler avatar dawnsheedy avatar dependabot[bot] avatar gloribus avatar gygabyyyte avatar hatsu38 avatar hawekotte avatar hlouzek avatar hqz3 avatar jclipa avatar jojonarte avatar mattmogford avatar mattmogford-alcumus avatar maxikucan avatar pat721 avatar salemaljebaly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-doc-viewer's Issues

SyntaxError: Cannot use import statement outside a module

Good morning,

I am currently trying to get react-doc-viewer to work with my react instance using Volto (a react frontend for Plone CMS). However, I am getting the following error:
`
/Users/anonymous/Plone/frontend/node_modules/@cyntler/react-doc-viewer/dist/index.js:16
import React from "react";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.@cyntler/react-doc-viewer (/Users/anonymous/Plone/frontend/external "@cyntler/react-doc-viewer":1:1)
at webpack_require (/Users/anonymous/Plone/frontend/build/webpack:/webpack/bootstrap:754:1)

`
How do I go about fixing this problem without modifying the node_module code itself?

I thank you kindly.

Sincerely,

rbrown10340a

Fix short loading message

We should to change the behaviour when we switch to the next/prev document - loading message should appears after some time after switch (500ms for example). It is good idea to avoid the problem with "jumping" when file change.

Error: require() of ES Module in v.1.10.0

Hello!
I get the following error in v.1.10.0

Error: require() of ES Module C:\Users...\node_modules\csv-parse\dist\esm\index.js from C:\Users...\node_modules@cyntler\react-doc-viewer\dist\cjs\plugins\csv\index.js not supported.
Instead change the require of C:\Users...\node_modules\csv-parse\dist\esm\index.js in C:\Users...\node_modules@cyntler\react-doc-viewer\dist\cjs\plugins\csv\index.js to a dynamic import() which is available in all CommonJS modules.

And if i downgrade to v.1.8.0 as was suggested by previous post I get the window is not defined bug instead.

Hope this is enough information to solve it!

Adding auth header

I need to add an authorization header to the file load. Is there an easy way that I can do that? I looked at the custom loader code and the internal fileLoader, but couldn't get anything I've tried so far to work. I want to use the built in renderers. Any help is appreciated.

What happens?

image
image

And also at the node modules I do not see ane index file with default export :
image

Displaying blob dosen't work

i have a blob return from API and use it with createObjectURL but it doesn't work

my blob
Capture

where i use
image

the result
image

is there any chance to display it? like base64 or something like that

Rendering Doc and Docx issues

I'm trying to get a file to render that is already an ArrayBuffer data format, but most of the solutions always render from a file, or in this case from the "URI".

I was looking into the Custom Renderer and Custom File Loader sections of the documentation to override how this is rendered or how the file is retrieved, but nothing seems to be working to get the ArrayBuffer data to display.

Is there a way to use this library to to take a doc or docx ArrayBuffer and render it directly without a filename or URI?

Hide some excel's sheets

i want to display only some worksheets from excel file to reacjs website,
i try to hide some worksheets in excel file but when display in website it always shows
i need some help thanks

content-type: application/octet-stream is not supported?

Hi,

I am trying to render doc file with content-type content-type: application/octet-stream but it is giving render error.

<DocViewer pluginRenderers={DocViewerRenderers} documents={[{ uri: "https://xyz/e49ac077985846ab9198340b51c18d2c.docx" }]} />

Screenshot from 2022-09-26 18-00-13

image urls (.png, jpg) are working fine.

can not preview local pdf file

i want to preview a local pdf file. but it shows this error.

Module not found: Error: You attempted to import C:\Users\Kaya\Desktop\kaya.pdf which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

thank you

Support to render current document

Can we add support to render the current document by passing an index or something from the list of documents that we pass as props?
or do we have configs for that in the current version?

Use blob content

Hi, I'm trying to implement the component on my platform but I get the documents (pdf, docx, xlsx,...) from the server as a blob object. I am having many problems to be able to visualize the documents.
Is there a way to deal with blob objects in this component? Thanks.

image

CORS error when fetch image from S3

image
image
image

*note: ImageDisplay is my custom image display

I faced this issue while working in my own project although in S3 i set Accpet-Control-Allow-Origin to '*' (image 3). I found out that if i write a fetch method to that s3 url its still face the same error but when i set mode: 'no-cors' it success. Anyone can explain why for me :( is there any way to work around or to set no-cors mode to reac-doc-viewer?

This is my first time submit an issue so plz appologize for any mistakes, thank you guys a lot.

Unable to render document view

Hi, i would like to ask how to resolve this problem:
obrázok

As you can see my file from AWS S3 bucket is fetched but viewer doesn't render content and i don't understand why content type is set to multipart/form-data

Thanks for answer.

require is not defined

i am trying to view pdf file which is in my local folder but when i try to put in docs array using require my react app is showing an error Uncaught ReferenceError: require is not defined

FilteType prop error on document?

Hi.

When defining a fileType for document:
const doc = [ { uri: 'http://....', fileType: 'image/png', fileName: 'test.png', }, ];

     I get render error:
     `No renderer for file type: image&#x2F;png`

Error: Blocked script execution in because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

I've been trying to figure out how to change the iframe to allow javascript to run.
image

heres the exact error i get:

index.js:20 Blocked script execution in 'http://localhost:3006/documents/123' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

i found this: https://stackoverflow.com/questions/24531061/blocked-script-execution-in-because-the-documents-frame-is-sandboxed-angular

but idk how i can modify the iframe to make it run javascript.

Not able to preview local file selected through input tag.

I am facing problem to show local files which user has selected throgh the input
let file = '';
<input type="file" accept=".pdf" id="" multiple onChange={(el) => file = el.target.files }/>
<DocViewer pluginRenderers={DocViewerRenderers} config={{ header: { retainURLParams: false } }} documents={[{ uri: file ? file[0] : '' }]}

Controls on images

Hello, I don't know if it's possible to enable controls (like on the pdf files) for the images, to download, zoom in and out.

If it's possible how can I enable it ? And if not could you implement this feature ?

Thanks for the help !

DataStream is not defined

Hi i am facing issue when using this library it is show me following error when i follow the first example in docs

Uncaught ReferenceError: DataStream is not defined
at DataStream.js:24:5
at DataStream.js:17:26
at node_modules/wl-msg-reader/lib/DataStream.js (DataStream.js:22:1)
at __require2 (chunk-UOOVSGC4.js?v=2d8e5a12:18:50)
at node_modules/wl-msg-reader/index.js (index.js:3:22)
at __require2 (chunk-UOOVSGC4.js?v=2d8e5a12:18:50)
at node_modules/@cyntler/react-doc-viewer/dist/plugins/msg/index.js (index.js:36:23)
at __require2 (chunk-UOOVSGC4.js?v=2d8e5a12:18:50)
at node_modules/@cyntler/react-doc-viewer/dist/index.js (index.js:62:29)
at __require2 (chunk-UOOVSGC4.js?v=2d8e5a12:18:50)

Displayes error for every file: An error occurred

An error occurred
We're sorry, but for some reason we can't open this for you.

It doesn't work with the demo files which are included in the project.
image

Node version: 16
"dependencies": { "@cyntler/react-doc-viewer": "^1.2.0", "@testing-library/jest-dom": "^5.16.4", "@testing-library/react": "^13.3.0", "@testing-library/user-event": "^13.5.0", "@types/jest": "^27.5.2", "@types/node": "^16.11.41", "@types/react": "^18.0.14", "@types/react-dom": "^18.0.5", "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", "typescript": "^4.7.4", "web-vitals": "^2.1.4" },

Add error management when fetching documents

I am currently using external APIs that sometimes get stuck during the prefetch, so I dont get che correct content type and I'm unable to show the document.
It will be nice to have a lifted up state that tells me that the fetch/prefetch failed so I can trigger a useEffect to re-render the component or to refetch

Warning: Setting up fake worker.

I get this error in console while I try to open a pdf file, local pdf files also. Warning: Setting up fake worker.

<DocViewer
                 pluginRenderers={DocViewerRenderers}

                 documents={[{ uri: require("C:/Users/username/Documents/app-web/src/components/test.pdf"), fileType: 'application/pdf' }]} />
                 

anyone else have this issue ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.