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/angular @cloudpdf/engine

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

It needs Angular 20.2 or later. Everything is standalone, OnPush and built on signals, so it works in zoneless apps (provideZonelessChangeDetection()), the default for new ones.

@angular/forms and @angular/cdk are optional. Install @angular/forms to use a form field as a FormControl (form.controlOf(ref)). Install @angular/cdk to test your components with the harnesses in @embedpdf/angular/testing: EpdfStageHarness reads the pages a Stage shows and its cursor, and turns the wheel; EpdfSearchLayerHarness counts the highlights on a page and clicks one.

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.

provideEmbedPdf() can also own the engine. Pass a function: the viewer calls it in the browser when it starts, and destroys the engine when the component or route that provides it goes away:

owned-engine-cloud.ts
import { Component } from '@angular/core';
import { provideEmbedPdf } from '@embedpdf/angular/runtime';
import { withRender } from '@embedpdf/angular/render';
import { withStage } from '@embedpdf/angular/stage';
import { cloudEngine } from '@cloudpdf/engine';

@Component({
  selector: 'app-document-viewer',
  providers: [
    provideEmbedPdf(
      { engine: () => cloudEngine({ baseUrl: 'https://pdf.example.com' }) },
      withStage(),
      withRender(),
    ),
  ],
  template: `<!-- your toolbar and pages -->`,
})
export class DocumentViewer {}

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#

Each plugin is a feature of provideEmbedPdf(), given with the engine. Put it in the providers of the component that shows the viewer:

plugins.ts
import { Component } from '@angular/core';
import { provideEmbedPdf } from '@embedpdf/angular/runtime';
import { withInteraction } from '@embedpdf/angular/interaction';
import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render';
import { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection';
import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage';
import { engine } from './pdf';

@Component({
  selector: 'app-document-viewer',
  imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer],
  providers: [
    provideEmbedPdf({ engine }, withStage(), withRender(), withInteraction(), withSelection()),
  ],
  template: `
    <epdf-stage>
      <ng-template epdfPage>
        <epdf-render-layer />
        <epdf-selection-layer />
      </ng-template>
    </epdf-stage>
  `,
})
export class DocumentViewer {}

That component and everything in its template share one viewer, and the component can inject() the plugins’ services itself. In app.config.ts or a route’s providers, the same call gives the whole app or route one viewer, so a toolbar in your header and the pages in your outlet work together. The viewer reads the engine and the features once, when it starts.

A setup that can’t work, such as a service with no viewer above it, throws an error with a number and its fix, like Angular’s own. Errors lists 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.

With Angular SSR, the viewer works as it is, and the server renders the page around it. To keep its code, the engine included, out of the first download, put it in a @defer (on viewport) block: it loads when it scrolls into view.

ssr.ts
import { Component } from '@angular/core';
import { DocumentViewer } from './document-viewer';

@Component({
  selector: 'app-contract-page',
  imports: [DocumentViewer],
  template: `
    @defer (on viewport) {
      <app-document-viewer />
    } @placeholder {
      <div style="height: 600px">Loading the viewer…</div>
    }
  `,
})
export class ContractPage {}

The server renders the @placeholder, and the deferred component loads in its own chunk with everything it imports. on viewport watches the placeholder, so give it the viewer’s height, and nothing moves when the viewer takes its place.

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.ts
import { Component, computed, input } from '@angular/core';
import { provideEmbedPdf } from '@embedpdf/angular/runtime';
import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render';
import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage';
import { fetchDocumentToken } from './api';
import { engine } from './pdf';

@Component({
  selector: 'app-document-viewer',
  imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer],
  providers: [provideEmbedPdf({ engine }, withStage(), withRender())],
  template: `
    <epdf-stage [document]="source()">
      <ng-template epdfPage>
        <epdf-render-layer />
      </ng-template>
    </epdf-stage>
  `,
})
export class DocumentViewer {
  readonly documentId = input.required<string>();

  protected readonly source = computed(() => {
    const id = this.documentId();
    return { kind: 'token' as const, token: () => fetchDocumentToken(id) };
  });
}

Providers can’t read a component’s inputs, so a document that depends on one goes to <epdf-stage [document]>. When the id changes, the viewer opens the new document and closes the old one.

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, when the viewer starts. To use another engine or other plugins, create the component that provides them again, for example under another route or an @if.

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