import { ChangeDetectionStrategy, Component, inject, input, linkedSignal, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, EpdfDocuments, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { DocumentInfo, FailedDocumentInfo, 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'; // [!doc-source welcome] const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // A file that isn't a PDF, so its document can't be opened. const broken: OpenInput = { kind: 'bytes', bytes: new TextEncoder().encode('Not a PDF') }; @Component({ selector: 'demo-password-form', changeDetection: ChangeDetectionStrategy.OnPush, template: `

{{ document().name }} needs a password

@if (wrong()) {

That password isn’t right. Try again.

}
`, }) export class PasswordForm { readonly document = input.required(); private readonly documents = inject(EpdfDocuments); protected readonly password = signal(''); protected readonly wrong = linkedSignal(() => this.document().passwordProvided ?? false); protected async unlock() { try { await this.documents.unlock(this.document().id, { password: this.password() }); } catch { this.wrong.set(true); // it stays locked; ask again } } } @Component({ selector: 'demo-open-error', changeDetection: ChangeDetectionStrategy.OnPush, template: `

Couldn’t open {{ document().name }}

{{ document().error.message }}

`, }) export class OpenError { readonly document = input.required(); protected readonly documents = inject(EpdfDocuments); } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, PasswordForm, OpenError, ], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [ { source: welcome, name: 'welcome.pdf' }, { source: broken, name: 'broken.pdf', active: true }, ], }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './states.css', encapsulation: ViewEncapsulation.None, template: `
@for (document of documents.documents(); track document.id) { }
Opening…
`, }) export class App { protected readonly documents = inject(EpdfDocuments); }