> ## 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.

# JS API in the browser

The facade is the same in a page. Two things change: there is no file system, and you supply the fonts.

## Edit only

If you only edit and save, import from `@reductoai/pptx/officejs`. It needs no fonts and no layout.

```ts theme={null}
import { PowerPointPresentation } from "@reductoai/pptx/officejs";

const input = document.querySelector<HTMLInputElement>("input[type=file]")!;
const file = input.files![0];

const deck = await PowerPointPresentation.fromBytes(new Uint8Array(await file.arrayBuffer()));
deck.slides.getItemAt(0).shapes.addTextBox("Approved", { left: 40, top: 40, width: 200, height: 40 });

const bytes = await deck.toBytes();
const blob = new Blob([new Uint8Array(bytes)], {
  type: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
});
```

## Lay out and render

To paint a deck in a page, lay out its bytes with `runPptxPipeline` from `@reductoai/pptx/browser`. Give the registry the faces first:

```ts theme={null}
import { defaultFontRegistry, portableFaceProvider, runPptxPipeline } from "@reductoai/pptx/browser";
import { renderPdfBytes } from "@reductoai/pptx/pdf";

const fonts = portableFaceProvider((file) => `/fonts/${file}`);
await fonts.install(); // registers every family; no face bytes yet

defaultFontRegistry.startFaceTrace();
let ir = await runPptxPipeline(bytes);
// Fetch the faces the layout asked for, then lay out again with them.
if (await fonts.fetch(defaultFontRegistry.takeFaceTrace())) ir = await runPptxPipeline(bytes);

const { bytes: pdf } = await renderPdfBytes(ir, defaultFontRegistry);
```

After an edit, call `deck.toBytes()` and lay out those bytes. Layout of a large deck takes time; run it in a module Web Worker so the page stays responsive.

## EMF, WMF and SVG pictures

Node renders these natively. A page needs a rasterizer, for example `@resvg/resvg-wasm`. Install the backend once, before the first layout:

```ts theme={null}
import { initWasm, Resvg } from "@resvg/resvg-wasm";
import resvgWasmUrl from "@resvg/resvg-wasm/index_bg.wasm?url"; // Vite; use your bundler's asset URL
import { installBrowserMetafileBackend, installBrowserSvgRasterizer } from "@reductoai/pptx/browser";

const ready = initWasm(fetch(resvgWasmUrl));

const backend = installBrowserMetafileBackend({
  async renderSvg(svg, { zoom, defaultFontFamily, fontBuffers }) {
    await ready;
    const resvg = new Resvg(svg, {
      fitTo: { mode: "zoom", value: zoom },
      font: { fontBuffers, defaultFontFamily, loadSystemFonts: false },
    });
    return new Uint8Array(resvg.render().asPng());
  },
});

installBrowserSvgRasterizer({
  backend,
  probe: async (svg) => {
    await ready;
    return new Resvg(svg);
  },
});
```

Chart SVGs name their fonts, so route SVG pictures through the backend: it fetches the faces each picture names. Without a backend, EMF and WMF pictures show as empty boxes. To keep large pictures off the page thread, also pass `spawnWorker` and serve the worker with `serveMetafileRaster` (same options as `renderSvg`).

## Differences from Node

| | Node | Browser |
| - | - | - |
| Open and save | `open(path)`, `save(path)` | `fromBytes(bytes)`, `toBytes()` |
| Fonts | System fonts or portable profile, automatic | A `FaceProvider` you pass |
| PDF | `toPdf(path?)` | `runPptxPipeline`, then `renderPdfBytes` |
| PNG | `toPng()` | Paint the layout to a canvas, or use the [editor](https://docs.reducto.ai/office/pptx/ui/editor.md) |
| `getImageAsBase64` | Built in | Set `presentation.imageRenderer` |
