# @dankom/react-file-previewer

> A browser/device-agnostic file previewer for PDF and image file types built on top of React-PDF.

Latest version **0.8.2** (published 2021-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dankom/react-file-previewer
pnpm add @dankom/react-file-previewer
yarn add @dankom/react-file-previewer
bun add @dankom/react-file-previewer
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.2 |
| Published | 2021-08-18 |
| First published | 2021-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 185.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Jonathan Stevens |
| Maintainers | dankom |
| Keywords | pdf viewer, file viewer, image viewer, react-pdf |

## Links

- npm: https://www.npmjs.com/package/@dankom/react-file-previewer
- Repository: https://github.com/vendorpay/react-file-previewer
- Homepage: https://github.com/vendorpay/react-file-previewer#readme
- Issues: https://github.com/vendorpay/react-file-previewer/issues
- npm.io page: https://npm.io/package/@dankom/react-file-previewer

## Dependencies (8)

- [ramda](https://npm.io/package/ramda.md) ^0.27.1
- [react-pdf](https://npm.io/package/react-pdf.md) ^5.3.2
- [file-saver](https://npm.io/package/file-saver.md) ^2.0.5
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.27
- [mdi-material-ui](https://npm.io/package/mdi-material-ui.md) ^6.22.1
- [@material-ui/core](https://npm.io/package/@material-ui/core.md) ^4.11.0
- [@material-ui/icons](https://npm.io/package/@material-ui/icons.md) ^4.9.1
- [react-intersection-observer](https://npm.io/package/react-intersection-observer.md) ^8.32.0

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 0.8.2 (latest) — 2021-08-18
- 0.8.1 — 2021-08-17
- 0.8.0 — 2021-08-10
- 0.7.1 — 2021-08-09
- 0.7.0 — 2021-08-09

## README

# react-file-previewer
A browser/device-agnostic file previewer for PDF and image file types built on top of React-PDF.

## Installation
```
npm i react-file-previewer
```

## Usage
This component supports URLs and base64 encoded data.

### Basic Usage
```javascript
import FilePreviewer from 'react-file-previewer';

export const App = () => (
    <div>
        <h1>My App</h1>
        <FilePreviewer file={{
            url: "https://cors-anywhere.herokuapp.com/http://africau.edu/images/default/sample.pdf"}}
        />
    </div>
);
```

### Base64 Usage
```javascript
import FilePreviewer from 'react-file-previewer';

export const App = () => (
    <div>
        <h1>My App</h1>
        <FilePreviewer 
            file={{
                data: "<base64 string>",
                mimeType: 'application/pdf',
                name: 'sample.pdf' // for download
            }}
        />
    </div>
);
```

### Using with HTML file input
```javascript
import { useState } from 'react';
import FilePreviewer from 'react-file-previewer';

const PDF1_URL =
  'https://cors-anywhere.herokuapp.com/http://africau.edu/images/default/sample.pdf';

export const App = () => {
    const [file, setFile] = useState({ url: PDF1_URL });
    
    const onFileChange = event => {
        const fileReader = new window.FileReader();
        const file = event.target.files[0];
        
        fileReader.onload = fileLoad => {
            const { result } = fileLoad.target;
            setFile({ url: result });
        };
        
        fileReader.readAsDataURL(file);
    };

    return (
        <div>
            <h1>My App</h1>
            <input type="file" onChange={onFileChange} />
            <FilePreviewer 
                file={file}
            />
        </div>
    )
};
```

### Using it as a thumbnail
This will render the first page of the file as a thumbnail. It takes the exact same props as the default export
`FilePreviewer`.

```javascript
import {FilePreviewerThumbnail} from 'react-file-previewer';

export const App = () => (
    <div>
        <h1>My App</h1>
        <FilePreviewerThumbnail 
            file={{
                data: "<base64 string>",
                mimeType: 'application/pdf',
                name: 'sample.pdf' // for download
            }}
        />
        -or-
        <FilePreviewerThumbnail file={{
            url: "https://cors-anywhere.herokuapp.com/http://africau.edu/images/default/sample.pdf"}}
        />
    </div>
);
```

## FilePreviewer Props

| Prop name    | Type     | Description |
|:-------------|:---------|:------------|
| file         | object   | Refer to [file object](#file-object) |
| onClick      | function | Event handler for when viewer is clicked |
| hideControls | bool  | If 'true', viewer won't display the zoom, page up/down, and fit-to-screen controls |

## FilePreviewerThumbnail Props

| Prop name    | Type     | Description |
|:-------------|:---------|:------------|
| file         | object   | Refer to [file object](#file-object) |
| onClick      | function | Event handler for when viewer is clicked |
| hideControls | bool  | If 'true', viewer won't display the zoom, page up/down, and fit-to-screen controls |
| style        | object  | Any css style overrides |

### File Object

| Prop name | Type   | Description |
|:----------|:-------|:------------|
| url       | string | This can be used by itself with no other prop |
| data      | string | Base64 encoded string of file. If used, `mimeType` must also be provided |
| mimeType  | string | Type of the file |
| name      | string | Used to specify the filename when download button is clicked |

---
_Source: https://npm.io/package/@dankom/react-file-previewer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
