CloudPDF
DocsPricing
Start building

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#

npm install @embedpdf/vue @cloudpdf/engine

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

It’s written for Vue 3: components for your templates and composables for <script setup>. It needs Vue 3.4 or later.

Create the engine once#

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

// 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.vue
<script setup lang="ts">
import { Viewer } from '@embedpdf/vue/runtime';
import { cloudEngine } from '@cloudpdf/engine';
import { plugins } from './pdf';
</script>

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

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
import { stagePlugin } from '@embedpdf/vue/stage';
import { renderPlugin } from '@embedpdf/vue/render';
import { interactionPlugin } from '@embedpdf/vue/interaction';
import { selectionPlugin } from '@embedpdf/vue/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 Nuxt, render the viewer on the client only, inside <ClientOnly>. The server renders its #fallback slot instead:

ssr.vue
<!-- pages/contract.vue -->
<template>
  <ClientOnly>
    <DocumentViewer />
    <template #fallback>
      <div style="height: 600px">Loading the viewer…</div>
    </template>
  </ClientOnly>
</template>

A component named DocumentViewer.client.vue renders on the client only wherever you use it, with no <ClientOnly> around it.

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.vue
<script setup lang="ts">
import { Viewer } from '@embedpdf/vue/runtime';
import { fetchDocumentToken } from './api';
import { engine, plugins } from './pdf';

defineProps<{ documentId: string }>();
</script>

<template>
  <Viewer
    :engine="engine"
    :plugins="plugins"
    :initial-documents="[{ source: { kind: 'token', token: () => fetchDocumentToken(documentId) } }]"
  />
</template>

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

Changing engine or plugins#

Both are read once. To use another engine or another set of plugins, mount a new <Viewer>, for example by giving it a new key.

The ready-made Viewer uses these same packages. If you start there, everything on these pages still applies when you move to headless.