import { ChangeDetectionStrategy, Component, inject, Injectable, signal, ViewEncapsulation, } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfActions, withActions, withActionsUi, type ActionContext, type PdfActionTree, } from '@embedpdf/angular/actions'; import { localEngine } from '@embedpdf/engine'; // [!doc-source links] const links = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/links.pdf'); return { kind: 'bytes', id: 'links', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // A JavaScript action, as a PDF carries it behind a button or a link. const script = (code: string): PdfActionTree => ({ root: { type: 'javascript', subtype: 'JavaScript', script: code, next: [] }, incomplete: false, warnings: [], }); const click: ActionContext = { origin: 'user', source: { kind: 'api' }, event: { type: 'activate' }, }; const GREETING = "app.alert('Hello, ' + identity.name + ' from ' + identity.corporation + '!');"; const MISTAKE = 'app.alrt("typo");'; interface OutputLine { kind: 'alert' | 'error'; text: string; } /** What the script said: its alerts and its errors, newest first. */ @Injectable() export class ScriptOutput { readonly lines = signal([]); show(kind: OutputLine['kind'], text: string) { this.lines.update((lines) => [{ kind, text }, ...lines].slice(0, 5)); } } // Edit the script and run it. Its alerts and its errors show below it. @Component({ selector: 'demo-script-console', changeDetection: ChangeDetectionStrategy.OnPush, template: `
    @for (line of output.lines(); track $index) {
  1. {{ line.text }}
  2. }
`, }) export class ScriptConsole { protected readonly actions = inject(EpdfActions); protected readonly output = inject(ScriptOutput); protected readonly code = signal(GREETING); protected readonly mistake = MISTAKE; constructor() { this.actions.scriptFailed$ .pipe(takeUntilDestroyed()) .subscribe(({ error }) => this.output.show('error', error.message)); // The greeting runs once on load. void this.actions.execute(script(GREETING), click); } protected run() { void this.actions.execute(script(this.code()), click); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, ScriptConsole], providers: [ ScriptOutput, provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: links }] }, // JavaScript on, and two fields scripts often ask for about the user. withActions({ javascript: { enabled: true, identity: { name: 'Dana Smith', corporation: 'Acme' } }, }), // A script's alert goes to your UI adapter: here, a line below the script. withActionsUi(() => { const output = inject(ScriptOutput); return { alert: (message) => output.show('alert', message) }; }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './javascript.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}