0.1.54 • Published 8 months ago

react-doc-render v0.1.54

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

react-doc-render

react-doc-render is a lightweight React library designed for rendering documents of various popular MIME types directly in the browser. The library supports formats such as PDF, images (JPEG, PNG, GIF, SVG, BMP, TIFF), text files (plain text, HTML, XML, CSV, JSON), Microsoft Office documents (Word, Excel), audio files (MP3, WAV, OGG, AAC), video files (MP4, WebM, OGG, AVI, QuickTime, MPEG, WMV), and ZIP archives. It provides a simple and unified interface to handle different content types.

Supported MIME types

ExtensionMIME typePassed
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
.apngimage/apng
.pngimage/png
.jpeg, .jpgimage/jpeg
.gifimage/gif
.bmpimage/bmp
.svgimage/svg+xml
.tifimage/tif
.tiffimage/tiff
.txttext/plain
.xmlapplication/xml
.xmltext/xml
.jsonapplication/json
.csvtext/csv
.htmltext/html
.pdfapplication/pdf
.xlsapplication/vnd.ms-excel
.xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.odsapplication/vnd.oasis.opendocument.spreadsheet
.zipapplication/zip
.zipapplication/x-zip-compressed
.mp4video/mp4
.webmvideo/webm
.oggvideo/ogg
.avivideo/x-msvideo
.movvideo/quicktime
.mpeg, .mpgvideo/mpeg
.wmvvideo/x-ms-wmv
.mp3audio/mpeg
.wavaudio/wav
.oggaudio/ogg
.mp4audio/mp4
.aacaudio/aac
.wavaudio/x-wav

MIME types are obtained through a separate request for the Content-Type header. You can pass the MIME type and file size directly to reduce the number of requests. This approach streamlines the process and enhances efficiency when handling various file types within the library.

Installation

To install the library, run the following command:

npm install react-doc-render

Copy PDF-worker to your public directory with .js extension:

cp node_modules/pdfjs-dist/build/pdf.worker.min.mjs public/pdf.worker.min.js

Usage

1. Import the Component

Import DocRender from the library:

import { DocRender } from 'react-doc-render';

2. Add the Component to the Render Section

Use the DocRender component in your application by passing the document URI:

<DocRender uri={uri} />

Configuration

The DocRender component accepts the following configuration options:

ParameterTypeRequiredDefaultDescription
uristringyesnullThe URI of the document to render.
loadingReact.FCno<>Loading...</>A component to display while the document is loading.
messageMessageFunctionno<MessageComponent text={text} type={type} />Returns a service message, e.g., firing a toast or rendering a component.
renderersRendererFunctionnoLibrary renderersCustom rendering functions for handling specific MIME types.
mimestringnonullTo specify the MIME type directly.
sizenumbernonullTo specify the file size in bytes directly.
limit{[key: string]: number}no{"application/zip": 1048576, "application/x-zip-compressed": 1048576}Limit for rendering the file MIME type in bytes.
i18n[languageCode: string]: {[key: string]: string;}noTranslated messagesTranslated service messages.
langen \| ru \| es \| zh \| ar \| frno'en'Language of service messages.
...otherPropsanynonullYou can pass any additional props that you want.

Examples of usage

Custom Loading component.

You can display a custom loading component instead of the predefined one.

import React from 'react';
import { DocRender } from "react-doc-render";

const CustomLoadingComponent: React.FC = () => {
    return <>Custom loading...</>;
};

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.docx"
                loading={CustomLoadingComponent}
            />
        </>
    );
};

export default App;

Message function

message attribute is for displaying a message instead of rendering the default component. It can be used, for example, for displaying alerts and toasts.

import React from 'react';
import { DocRender } from "react-doc-render";
import { MessageFunction } from 'react-doc-render/dist/types';

const customMessage: MessageFunction = (text, type) => {
    console.log(`${type}: ${text}`);
    alert(`${type}: ${text}`);
};

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.docx"
                message={customMessage}
            />
        </>
    );
};

export default App;

Custom renderers functions.

Predefined renderers can be replaced or new ones can be added.

import React from 'react';
import { DocRender } from "react-doc-render";
import { RendererFunction } from 'react-doc-render/dist/types';

const myCustomYmlRendererFunction: RendererFunction = async (buffer, setContent, mimeType) => {
    const text = new TextDecoder().decode(buffer);
    const content = `<p>Output from my custom ${mimeType}-renderer:</p><pre>${text}</pre>`;
    const html = `<div id="rdr-content" class="rdr-content-myCustomYmlRendererFunction">${content}</div>`;
    const callback = () => console.log(`${mimeType}-file was successfully rendered`);
    setContent({ html, callback });
};

const customRenderers = {
    'text/yaml': myCustomYmlRendererFunction,
};

const App = () => {
    return (
        <>
            <DocRender
                uri="./docker-compose.yml"
                renderers={customRenderers}
            />
        </>
    );
};

export default App;

Pass MIME type and file size directly.

If the file size and MIME type are known, they can be specified directly to avoid additional HEAD requests to the server. For this, both parameters must be applied simultaneously.

import React from 'react';
import { DocRender } from "react-doc-render";

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.docx"
                mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                size={17299}
            />
        </>
    );
};

export default App;

Limit

A limit can be added for file MIME type requests.

import React from 'react';
import { DocRender } from "react-doc-render";

const limit = {
    "application/zip": 5242880,
    "application/x-zip-compressed": 5242880
}

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.zip"
                limit={limit}
            />
        </>
    );
};

export default App;

Internationalization.

You can modify the default messages for predefined languages, as well as add support for other languages. Additionally, you can force a language for the messages.

import React from 'react';
import { DocRender } from "react-doc-render";

const i18n = {
    "de": {
        "error_fetching_the_file": "Fehler beim Abrufen der Datei",
        "unsupported_file_format": "Nicht unterstütztes Dateiformat",
        "unable_to_detect_the_files_mime_type": "Kann den MIME-Typ der Datei nicht erkennen",
        "file_size_exceeds_the_limit": "Dateigröße überschreitet das Limit"
    }
};

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.docx"
                i18n={i18n}
                lang="de"
            />
        </>
    );
};

export default App;

Styling.

Inside each renderer, there is a wrapper for the content with the ID #rdr-content and the CSS class .rdr-content-${name} available 'out of the box'. You can also assign classes and styles to the component itself.

import React from 'react';
import { DocRender } from "react-doc-render";

const App = () => {
    return (
        <>
            <DocRender
                uri="https://example.com/files/example.docx"
                className="mx-auto"
                style={{ position: 'absolute' }}
            />
        </>
    );
};

export default App;
0.1.54

8 months ago

0.1.53

8 months ago

0.1.52

8 months ago

0.1.51

8 months ago

0.1.50

8 months ago

0.1.49

8 months ago

0.1.48

8 months ago

0.1.47

9 months ago

0.1.46

9 months ago

0.1.45

9 months ago

0.1.44

9 months ago

0.1.43

9 months ago

0.1.42

9 months ago

0.1.41

9 months ago

0.1.40

9 months ago

0.1.39

9 months ago

0.1.38

9 months ago

0.1.37

9 months ago

0.1.36

9 months ago

0.1.35

9 months ago

0.1.34

9 months ago

0.1.33

9 months ago

0.1.32

9 months ago

0.1.31

9 months ago

0.1.30

9 months ago

0.1.29

9 months ago

0.1.28

9 months ago

0.1.27

9 months ago

0.1.26

9 months ago

0.1.25

9 months ago

0.1.24

9 months ago

0.1.23

9 months ago

0.1.22

9 months ago

0.1.21

9 months ago

0.1.20

9 months ago

0.1.19

9 months ago

0.1.18

9 months ago

0.1.17

9 months ago

0.1.16

9 months ago

0.1.15

9 months ago

0.1.14

9 months ago

0.1.13

9 months ago

0.1.12

9 months ago

0.1.11

9 months ago

0.1.10

9 months ago

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago