import { ChangeDetectionStrategy, Component, inject, Injectable, input, signal, 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 { EpdfActions, withActions, withActionsUi, type PdfNamedAction, } from '@embedpdf/angular/actions'; import { cloudEngine } from '@cloudpdf/engine'; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; const VERBS: ReadonlyArray<[PdfNamedAction, string]> = [ ['FirstPage', '⇤ First'], ['PrevPage', '‹ Previous'], ['NextPage', 'Next ›'], ['LastPage', 'Last ⇥'], ['Print', 'Print'], ]; /** What the last action did. */ @Injectable() export class LastRun { readonly text = signal(null); } // The viewer actions a PDF's buttons name, run from your own buttons. @Component({ selector: 'demo-named-actions', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (verb of verbs; track verb[0]) { } Page {{ stage().currentPageIndex() + 1 }} of {{ stage().pageCount() }} @if (ran.text(); as text) { · {{ text }} }
`, }) export class NamedActions { readonly stage = input.required(); protected readonly actions = inject(EpdfActions); protected readonly ran = inject(LastRun); protected readonly verbs = VERBS; constructor() { // On load, as if a "Last page" button in the PDF was clicked. Running it twice changes nothing. void this.actions .executeNamed('LastPage') .then(({ status }) => this.ran.text.set(`LastPage: ${status}`)); } protected async run(name: PdfNamedAction) { const { status } = await this.actions.executeNamed(name); if (name !== 'Print') this.ran.text.set(`${name}: ${status}`); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, NamedActions], providers: [ LastRun, provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withActions(), // Print goes to your UI adapter. This one says so, instead of opening the browser's print dialog. withActionsUi(() => { const ran = inject(LastRun); return { print: () => ran.text.set('Print: your print handler ran') }; }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './run-from-code.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}