> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Canvas viewer

This page builds a read-only document viewer. There is no editor here: you lay out the document, then paint the layout on canvases.

```sh theme={null}
npm install @reductoai/docx @reductoai/viewer
```

## 1. Lay out

With a bundler other than Vite, webpack or Next.js, first give the engine's URL. See [The layout engine](https://docs.reducto.ai/office/docx/js-api/browser.md#the-layout-engine).

```ts theme={null}
import { layoutDocxInBrowser, portableFaceProvider } from "@reductoai/docx/browser";

const fonts = portableFaceProvider((file) => `/fonts/${file}`);

const ir = await layoutDocxInBrowser(bytes, {
  fonts,
  name: "report.docx",
  onProgress: (event) => console.log(event),
});

console.log(ir.pages.length, ir.pages[0].width, ir.pages[0].height); // sizes in 1/100 mm
```

`ir` is a `LayoutDocument`: pages with positioned lines, runs, table cells, images and vector shapes. Everything is placed; a painter only draws.

Do this in a worker for long documents. The IR is plain data, so `postMessage` can send it to the page.

## 2. Load fonts and images

```ts theme={null}
import { loadFonts, loadImages } from "@reductoai/viewer/canvas-host";

await loadFonts(ir, (id) => fonts.bytes(id));
const images = await loadImages(ir);
```

`loadFonts` registers each face that the layout used as a `FontFace`, under a private name. Pass the provider's bytes, or the canvas falls back to a local font and the text no longer fits the layout.

## 3. Paint one page

```ts theme={null}
import { defaultLayers, drawPage, drawTextLayer } from "@reductoai/viewer/canvas-host";
import "@reductoai/viewer/viewer.css";

const canvas = document.createElement("canvas");
const layers = defaultLayers();
const { displayW } = drawPage(canvas, ir.pages[0], images, layers, { fitWidth: 800 });
document.body.append(canvas);

// Optional: transparent text over the bitmap, for select, copy and Ctrl+F
const text = document.createElement("div");
text.className = "text-layer";
drawTextLayer(text, ir.pages[0], layers, { displayW });
```

`drawPage` sizes the canvas buffer for the device pixel ratio. `fitWidth` is the CSS width; the aspect ratio stays the same. For thumbnails, use `drawThumb(canvas, page, images, layers, { fitWidth: 160 })`.

Put the text layer on top of the canvas, inside a `position: relative` box.

## 4. Or use the scrolling viewer

`DocumentPreviewer` does the rest: a page stack that scrolls, zoom (fit width, fit page, or a percent), paint only of the pages in view, sharp tiles at any zoom, and pinch zoom.

```ts theme={null}
import { DocumentPreviewer } from "@reductoai/viewer/previewer";

const pagesEl = document.querySelector<HTMLElement>("#pages")!; // a scroll container with a height
const viewer = new DocumentPreviewer(pagesEl, (info) => {
  label.textContent = `${info.pageLabel} · ${info.zoomLabel}`;
}, { textLayer: true });

await viewer.show(ir, images);

viewer.setZoom("fit");   // "fit" | "page" | 1.25
viewer.goToPage(2);      // 0-based
viewer.destroy();        // when you unmount
```

```css theme={null}
#pages {
  height: 100vh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  align-items: safe center;
  padding: 24px;
  background: #f3f3f3;
}
.page-card { margin-bottom: 24px; }
.page-shell { position: relative; box-shadow: 0 1px 4px rgb(0 0 0 / 0.15); }
```

## 5. Show page 1 before the layout ends

For long documents, paint pages as the layout releases them. Pass a `pageSink`:

```ts theme={null}
import { buildFaceManifest, type LayoutDocument } from "@reductoai/docx/browser";
import { addFonts, loadImages, type DecodedImages } from "@reductoai/viewer/canvas-host";

let images: Promise<DecodedImages>;
let imageTable: LayoutDocument["images"];

const ir = await layoutDocxInBrowser(bytes, {
  fonts,
  pageSink: {
    begin: (header) => {
      // The page count is not known yet. The header has images and bookmarks.
      imageTable = header.images;
      images = loadImages(header);
    },
    page: async (page) => {
      const manifest = buildFaceManifest({ images: imageTable, pages: [page] }) ?? {};
      await addFonts(manifest, (id) => fonts.bytes(id));
      paintWhenVisible(page, await images);
    },
    discard: () => clearPages(), // the first pass found missing fonts; pages come again
  },
});
```

With a sink, the result has `pages: []`; the pages went to the sink.

In a worker, do the same split: call `layoutDocxInBrowser` with a sink in the worker, and post each page to the page with its manifest and the face bytes it needs (`fonts.bytes(id)` for each `file` in the manifest). On the page, call `addFonts(manifest, bytesFor)` before you paint. Send a face once; the page keeps it.

## Track changes and comments in the viewer

Pass the view to the layout:

```ts theme={null}
await layoutDocxInBrowser(bytes, { fonts, trackedChanges: "inline", comments: "balloons" });
```

`"balloons"` scales each page and adds a markup margin, as Word prints with *All Markup*.

## PowerPoint

`@reductoai/pptx` lays out a .pptx deck to the same `LayoutDocument`. The same viewer, PDF writer and PNG export work for slides. See [/pptx/](https://docs.reducto.ai/office/pptx/getting-started/overview.md).
