Installation and first render
Two minutes from an empty project to a Word document on screen, with no build configuration and no server.
Install
One package is enough. It pulls in the facade, the parsers and the renderers as ordinary dependencies — all of them first-party, none of them with dependencies of their own.
npm install @apertura/reactRender a document
file accepts a File from an input, a Blob, a byte buffer, or a URL string. The format is detected from the bytes rather than from the file extension, so a mislabelled file still opens.
import { DocumentViewer } from '@apertura/react';
import { useState } from 'react';
export function App() {
const [file, setFile] = useState<File>();
return (
<div style={{ height: '100vh', display: 'grid', gridTemplateRows: 'auto 1fr' }}>
<input
type="file"
accept=".docx,.xlsx,.pptx"
onChange={(event) => setFile(event.target.files?.[0])}
/>
<DocumentViewer file={file} fit="width" />
</div>
);
}Server-side rendering
The renderer measures real line boxes to paginate, so it needs a layout engine and has nothing to do on the server. Under Next.js, Nuxt or Remix, load it in the browser only:
'use client';
import dynamic from 'next/dynamic';
const DocumentViewer = dynamic(
() => import('@apertura/react').then((m) => ({ default: m.DocumentViewer })),
{ ssr: false },
);Keeping the bundle small
The default registry includes every format. When an application only ever shows one, build the registry by hand and the other parsers never enter the bundle:
import { FormatRegistry } from '@apertura/core';
import { docxParser } from '@apertura/docx';
import { docxView } from '@apertura/docx-view';
export const registry = new FormatRegistry()
.registerParser(docxParser)
.registerView(docxView);
// Then: <DocumentViewer file={file} registry={registry} />