Quick start
This guide builds a small PDF viewer: the pages, text people can select, zoom buttons, a highlighter and a save button. Each step adds one plugin or one control, so you can stop at any step and have a working viewer.
1. Install#
You need two packages: the one for your framework, and an engine.
npm install @embedpdf/svelte@next @embedpdf/engine@nextEvery feature is a separate import from the framework package, such as @embedpdf/svelte/stage,
so what you don’t use never reaches your bundle.
2. Show a PDF#
Create the engine once, then give it to <Viewer> with the plugins you want and the document to
open. <Stage> shows the pages, and <RenderLayer> draws each one:
Loading live preview…
<script lang="ts" module>
import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
import { localEngine } from '@embedpdf/engine';
// The engine is created synchronously and costs nothing until first use, so
// a module-scope `const engine = …` is safe — even under SSR. Only opening a
// document does real work: the UI renders at t≈0.
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin()];
// The local engine opens bytes: fetch lazily, under the loading tab.
const ebook = async (): Promise<OpenInput> => {
const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};
</script>
<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
<div style="height: 500px">
<!-- Document UI is defined over a document — gate it on having one. -->
<DocumentGate>
{#snippet fallback()}
<p>Loading…</p>
{/snippet}
<Stage style="height: 100%">
<RenderLayer />
</Stage>
</DocumentGate>
</div>
</Viewer>localEngine() costs nothing until it’s used, so creating it at the top of a module is safe, even
with server-side rendering. The viewer starts it when it mounts: PDFium boots in a Web Worker in
the background, and the first page shows as soon as the document is open. The engine’s
WebAssembly is part of your build and loads from your own site; see Setup.
<DocumentGate> shows its fallback until the document is open, so the Stage never renders
without one.
3. Let people select text#
Add the interaction plugin, which handles the pointer for every feature, and the selection plugin.
Then put <SelectionLayer> above the page:
<script lang="ts">
import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
import { interactionPlugin } from '@embedpdf/svelte/interaction';
import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
import { SelectionLayer, selectionPlugin } from '@embedpdf/svelte/selection';
import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
import { localEngine } from '@embedpdf/engine';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin()];
const ebook = async (): Promise<OpenInput> => {
const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};
</script>
<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
<DocumentGate>
<Stage style="height: 500px">
<RenderLayer />
<SelectionLayer />
</Stage>
</DocumentGate>
</Viewer>Drag over text to select it, double-click a word, or triple-click a line. On a phone, long-press a word. See Selecting & copying for copying and a selection menu.
4. Add zoom buttons#
Every plugin has a function for its API and one for its state. useStage() does things,
and useStageState() gives you what to show:
<script lang="ts">
import { useStage, useStageState } from '@embedpdf/svelte/stage';
const stage = useStage();
const state = useStageState();
</script>
<div>
<button onclick={() => stage.zoomOut()}>−</button>
<span>{Math.round(state.zoomLevel * 100)}%</span>
<button onclick={() => stage.zoomIn()}>+</button>
</div>Put <ZoomControls /> anywhere inside <DocumentGate>. It updates when the zoom
changes, whether a button, a pinch or code changed it.
5. Add a highlighter#
The annotation plugin shows the annotations a PDF already has, and lets people add their own. Add it with its layer, and a button that turns on the highlight tool:
<script lang="ts">
import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
import { interactionPlugin } from '@embedpdf/svelte/interaction';
import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
import { SelectionLayer, selectionPlugin } from '@embedpdf/svelte/selection';
import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
import { localEngine } from '@embedpdf/engine';
import HighlightButton from './HighlightButton.svelte';
const engine = localEngine();
const plugins = [
stagePlugin(),
renderPlugin(),
interactionPlugin(),
selectionPlugin(),
annotationPlugin(),
];
const ebook = async (): Promise<OpenInput> => {
const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
};
</script>
<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
<DocumentGate>
<HighlightButton />
<Stage style="height: 500px">
<RenderLayer />
<SelectionLayer />
<AnnotationLayer />
</Stage>
</DocumentGate>
</Viewer>With the tool on, select text and it’s highlighted. Click a highlight with the pointer tool to
select it. The page picture leaves the annotations out by itself, because the annotation layer
draws them now. See Annotations for every
tool.
6. Save#
documents.download() gives you the PDF with every change, as bytes. saveFile hands them to the
browser as a download:
<script lang="ts">
import { saveFile, useDocuments } from '@embedpdf/svelte/runtime';
const documents = useDocuments();
async function save() {
saveFile(await documents.download(), 'reviewed.pdf');
}
</script>
<button onclick={save}>Save</button><SaveButton /> anywhere inside <Viewer>.
Next steps#
Your feedback goes directly to the documentation team.