Put it in your app Only to read: the viewer
A document only to read
@rebodocs/viewer is for an application that shows documents and does not edit them: a file manager, a preview, a page where a client reads a contract. The pages are those of the editor, read only, with a bar to zoom and print, and the first page of a document as a thumbnail.
See it
The sample agreement in DocsViewer, and its first page made into a picture beside it, in this page.
Opening the viewer…
The bar over the pages says the page in view, as they scroll; its views do what the buttons above do. Nothing on the pages can be selected to change: there is no caret.
The smallest page
The bytes of a .docx and an element with a height: there is no document to read first.
import { createViewer, setLocale } from '@rebodocs/viewer';
setLocale('es');
const bytes = new Uint8Array(await (await fetch('/contract.docx')).arrayBuffer());
const viewer = createViewer(document.querySelector('#pages')!, bytes); // #pages has a height
viewer.setLayout('all');
viewer.print();
viewer.destroy();In React, from the entry of its own; it is safe to render on a server, where it draws an empty element, and mounts in the browser:
'use client';
import { DocsViewer } from '@rebodocs/viewer/react';
export function Contract({ file }: { file: Uint8Array }) {
return (
<div style={{ height: '80vh' }}>
<DocsViewer docx={file} locale="es" onReady={(viewer) => (printer.current = viewer)} />
</div>
);
}React is an optional peer: npm does not install it for a page without React. An application that already has @rebodocs/react has the same DocsViewer in it, and needs nothing more.
What it has
- createViewer(element, docx, { layout?, bar? })
- The document read only in an element that has a size. docx is the bytes of a .docx (Uint8Array or ArrayBuffer) or a Doc already read. layout: "pages", as it comes, or "all"; bar: false to leave the bar out.
- viewer.setLayout("pages" | "all")
- One page under another, each whole on the screen; or every page at once, as large as lets them fit, down to a least size for a long document.
- viewer.print()
- The print dialog of the browser, with the pages as they are laid out: to paper, or to a PDF.
- viewer.pages, viewer.layout
- How many pages it has, and how they are set out now.
- viewer.editor
- The Editor that draws the pages, read only: its zoom, its fit, its events, for an application that leaves the bar out.
- viewer.destroy()
- Takes it away, with its bar.
- <DocsViewer docx layout? bar? locale? onReady? />
- The same, in React, from @rebodocs/viewer/react. It takes the height of what it is in; onReady gives the viewer, and nothing once it is gone.
- pagePicture(docx, size, { type?, quality? })
- The first page as a picture, a JPEG unless type says "image/png", its longest side size pixels.
- pageThumbnail(docx)
- The first page as the element the editor draws, at its real size (data-width and data-height say it), its pictures in it as data: to show made smaller.
- setLocale, addLocale, addFonts
- The language of the bar, and the fonts a document is written in when the system has not got them.
Thumbnails
The first page as the editor draws it: laid out out of sight, one document at a time, and drawn on a canvas with its pictures.
import { pagePicture } from '@rebodocs/viewer';
const jpeg = await pagePicture(bytes, 480); // a Blob: its longest side 480 pixels
img.src = URL.createObjectURL(jpeg);
await upload(`thumbnails/${id}.jpg`, jpeg); // or keep it beside the fileIt needs a browser: a picture is made in the page that has the file, and kept by the application if it wants it again. A font the document carries is drawn in the nearest one the system has.
On a phone
The pages are the same on a phone, only smaller: one fits the width of the screen, and the text is not set out again for it.
The buttons of the bar make the pages larger, and the pages are then moved sideways with a finger; what does not fit in the bar is left out, what matters least first. apps/viewer in the repository is a shelf of documents with their thumbnails, made to be tried on a phone: its server listens on the network.
What it weighs
It brings @rebodocs/editor and @rebodocs/core with it, and a bundler keeps of them what the viewer uses.
Built into an application, createViewer is about 160 KB compressed with gzip, and the whole editor with its interface about 260 KB. Most of it is the engine that lays the pages out as Word does, which a viewer needs as much as an editor.