import { ChangeDetectionStrategy, Component, effect, signal, untracked, viewChild, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { localEngine } from '@embedpdf/engine'; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; interface Entry { id: number; event: string; detail: string; } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './events.css', encapsulation: ViewEncapsulation.None, template: `
    @for (entry of entries(); track entry.id) {
  1. {{ entry.event }} {{ entry.detail }}
  2. }

Loading…

`, }) export class App { protected readonly entries = signal([]); private readonly stage = viewChild(EpdfStage); private count = 0; constructor() { effect((onCleanup) => { const stage = this.stage(); if (!stage) return; const subscriptions = [ stage.pageChanged$.subscribe(({ pageIndex, previousPageIndex }) => this.log('pageChanged$', `page ${previousPageIndex + 1} → ${pageIndex + 1}`), ), stage.zoomChanged$.subscribe(({ level, mode }) => this.log('zoomChanged$', `${Math.round(level * 100)}%, ${mode}`), ), stage.motionEnded$.subscribe(({ camera }) => this.log('motionEnded$', `at rest, ${Math.round(camera.zoom * 100)}%`), ), stage.viewportChanged$.subscribe(({ size }) => this.log('viewportChanged$', `${Math.round(size.width)} × ${Math.round(size.height)}`), ), ]; onCleanup(() => subscriptions.forEach((subscription) => subscription.unsubscribe())); }); // Glide to the second page on load, so the log has something to show. effect(() => { const stage = this.stage(); if (!stage || stage.pageCount() === 0) return; untracked(() => stage.goToPage(1)); }); } private log(event: string, detail: string) { this.entries.update((current) => [{ id: this.count++, event, detail }, ...current].slice(0, 6)); } }