---
title: "Setup — Svelte"
description: "Install the headless packages for your framework, create the engine once, and use them with server-side rendering."
framework: "Svelte"
other_frameworks:
  "React": "https://www.cloudpdf.com/docs/headless/react/setup.md"
  "Vue": "https://www.cloudpdf.com/docs/headless/vue/setup.md"
  "Angular": "https://www.cloudpdf.com/docs/headless/angular/setup.md"
source: "https://www.cloudpdf.com/docs/headless/svelte/setup"
---

# Setup

A headless viewer needs two packages: the one for your framework, and an engine. This page
installs them, creates the engine for your whole app, and covers server-side rendering, bundlers
and fonts.

## Install

```sh
pnpm add @embedpdf/svelte @cloudpdf/engine
```

The framework package holds every plugin, one import path each (`@embedpdf/svelte/search`,
`@embedpdf/svelte/annotation`). A plugin you don't import isn't in your bundle.

It's written for Svelte 5: components that take snippets, and functions that give you reactive
state for your runes. It needs Svelte 5.29 or later.

## Create the engine once

Create one engine when your app starts, and share it with every viewer:

```ts
// pdf.ts
import { cloudEngine } from '@cloudpdf/engine';

export const engine = cloudEngine();
```

It connects to CloudPDF SaaS. Your own server names itself:
`cloudEngine({ baseUrl: 'https://pdf.example.com' })`. Creating the engine makes no request.

`<Viewer>` can also own the engine. Pass a function, and the viewer creates the engine when it
mounts and destroys it when it unmounts:

**`owned-engine-cloud.svelte`**

```svelte
<script lang="ts">
  import { Viewer } from '@embedpdf/svelte/runtime';
  import { cloudEngine } from '@cloudpdf/engine';
  import { plugins } from './pdf';
</script>

<Viewer engine={() => cloudEngine({ baseUrl: 'https://pdf.example.com' })} {plugins} />
```

An engine you pass in yourself is never destroyed by the viewer, so one engine can serve every
viewer in your app.

## Register the plugins

Plugins are a list you create once, next to the engine:

**`plugins.ts`**

```typescript
import { stagePlugin } from '@embedpdf/svelte/stage';
import { renderPlugin } from '@embedpdf/svelte/render';
import { interactionPlugin } from '@embedpdf/svelte/interaction';
import { selectionPlugin } from '@embedpdf/svelte/selection';

export const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin()];
```

The viewer reads `engine` and `plugins` once, when it mounts, and ignores later changes to them.

Order doesn't matter, and a plugin that needs another says so when the app starts: the annotation
plugin, for example, needs the interaction plugin.

## Server-side rendering

The headless packages render nothing on the server that needs the engine: the engine only starts in
the browser. So a module-level engine is safe everywhere.

In SvelteKit, render the viewer in the browser only, for example behind `{#if browser}` with
`browser` from `$app/environment`:

**`ssr.svelte`**

```svelte
<!-- src/routes/contract/+page.svelte -->
<script lang="ts">
  import { browser } from '$app/environment';
  import DocumentViewer from '$lib/DocumentViewer.svelte';
</script>

{#if browser}
  <DocumentViewer />
{:else}
  <div style="height: 600px">Loading the viewer…</div>
{/if}
```

To leave a whole page out of server rendering, add `export const ssr = false` to its `+page.ts`.

## Tokens

Each document opens with a token your backend creates: it says who the user is and what they may
do. Pass a function: the engine calls it for a token, and again a minute before that token
expires, so a long session never runs out:

**`tokens.svelte`**

```svelte
<script lang="ts">
  import { Viewer } from '@embedpdf/svelte/runtime';
  import { fetchDocumentToken } from './api';
  import { engine, plugins } from './pdf';

  let { documentId }: { documentId: string } = $props();
</script>

<Viewer
  {engine}
  {plugins}
  initialDocuments={[{ source: { kind: 'token', token: () => fetchDocumentToken(documentId) } }]}
/>
```

[Authentication](https://www.cloudpdf.com/docs/engine/authentication.md) covers the three ways to open a document and what a
token carries. Fonts, limits and storage are your server's configuration.

## Good to know

### One viewer, many documents

A viewer can hold several open documents at once, like tabs. See
[Several documents](https://www.cloudpdf.com/docs/headless/svelte/documents/tabs.md).

### Changing `engine` or `plugins`

Both are read once. To use another engine or another set of plugins, mount a new `<Viewer>`, for
example inside a `{#key}` block.

> The ready-made [Viewer](https://www.cloudpdf.com/docs/viewer/svelte.md) uses these same packages. If you start there, everything on
> these pages still applies when you move to headless.
