import { ChangeDetectionStrategy, Component, computed, effect, inject, input, output, 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 { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfForm, EpdfFormLayer, toFieldRef, withForm } from '@embedpdf/angular/form'; import { EpdfStamp, withStamp } from '@embedpdf/angular/stamp'; import { createTestSigner, EpdfSignature, withSignature } from '@embedpdf/angular/signature'; import { cloudEngine } from '@cloudpdf/engine'; import { localEngine } from '@embedpdf/engine'; const engine = cloudEngine(); const assetEngine = localEngine(); // A throwaway key for the demo. Bring your own with `webCryptoSigner`, a // service with `remoteSigner`, or a person's own with `personalSigner`. const signer = createTestSigner({ commonName: 'Ada Lovelace' }); const agreement: OpenInput = { kind: 'grant', grant: 'shr_G5GcxJeEBa2IiwIs0DCNruCL' }; // A mark's button: its picture once there is one, its label until then. @Component({ selector: 'button[demoMarkButton]', changeDetection: ChangeDetectionStrategy.OnPush, host: { type: 'button', class: 'button', '[title]': 'label()', '(click)': 'pick.emit()' }, template: ` @if (url(); as url) { } @else { {{ label() }} } `, }) export class MarkButton { readonly assetId = input.required(); readonly label = input.required(); readonly pick = output(); protected readonly url = inject(EpdfStamp).previewUrlOf(() => this.assetId()); } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfFormLayer, MarkButton, ], providers: [ provideEmbedPdf( { engine, initialDocuments: [{ source: agreement }] }, withStage(), withRender(), withInteraction(), withForm(), withStamp({ assetEngine }), withSignature({ signingKey: () => signer, // Trust the demo key itself, so its signatures check out as 'valid'. trust: { anchors: async () => [(await signer).certificate] }, }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './basic.css', encapsulation: ViewEncapsulation.None, template: `
@for (asset of person()?.signatures; track asset.id) { } {{ status() }}

Loading…

`, }) export class App { private readonly form = inject(EpdfForm); private readonly stamp = inject(EpdfStamp); private readonly signature = inject(EpdfSignature); private readonly stage = viewChild(EpdfStage); private done = false; protected readonly person = computed(() => this.signature.signerRows()[0]); private readonly field = computed(() => this.form.fields().find((candidate) => candidate.kind === 'signature'), ); private readonly error = signal(null); protected readonly status = computed(() => { // Ada's signature, once she signs: the agreement already carries Lanternfield's. const signed = this.signature.signatures().find((each) => each.fieldName === 'approval'); const error = this.error(); if (!this.person() || !this.field()) return 'Getting ready…'; if (error) return error; if (this.signature.busy()) return 'Signing…'; if (signed) return `Signed by ${signed.signer.name}: ${signed.verdict?.summary ?? 'checking…'}`; if (this.signature.target()) return 'Now pick the signature'; return 'Click the field, or pick the signature'; }); constructor() { // Start at the agreement's signature field. The browser holds no signatures // yet: add one person with a typed signature. effect(() => { const stage = this.stage(); if (this.form.status() !== 'ready' || !stage || this.done) return; this.done = true; untracked(() => void this.setUp(stage)); }); } /** The target is the field someone clicked; without one, the form's signature field. */ protected sign(assetId: string) { const destination = this.signature.target() ?? this.field()?.ref; if (!destination) return; this.error.set(null); this.signature .placeMark({ assetId }, { field: destination }) .catch((reason: Error) => this.error.set(reason.message)); } private async setUp(stage: EpdfStage) { // Bring the agreement's signature field into view. const first = this.form.get(toFieldRef('approval'))?.widgets[0]; const widget = first ? this.form.getWidget(first) : null; if (widget) stage.reveal(widget.page, { rect: widget.rect }); const { library } = await this.stamp.createLibrary('Ada Lovelace', { kind: 'signatures' }); await this.stamp.createAsset({ libraryId: library.id, name: 'signature', label: 'Signature', mark: { kind: 'text', text: 'Ada Lovelace', fontFamily: 'times-italic', color: '#1d2b53' }, }); } }