import { ChangeDetectionStrategy, Component, DestroyRef, inject, input, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf, saveFile } 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 { EpdfSelectionLayer, withSelection } from '@embedpdf/angular/selection'; import { withInteraction } from '@embedpdf/angular/interaction'; import { EpdfAnnotationLayer, withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfStamp, indexedDbByteStore, persistStampLibraries, restoreStampLibraries, withStamp, type StampAsset, type StampLibrary, } from '@embedpdf/angular/stamp'; import { localEngine } from '@embedpdf/engine'; const engine = localEngine(); const assetEngine = engine; // stamp libraries are PDFs; they open here too const approvals = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/approvals.pdf'); return { kind: 'bytes', id: 'approvals', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // Where the libraries live between visits: this browser's IndexedDB. const store = indexedDbByteStore('embedpdf-stamp-example'); @Component({ selector: 'button[demoStampButton]', changeDetection: ChangeDetectionStrategy.OnPush, host: { type: 'button', class: 'button', '[title]': "'Place “' + asset().label + '”'", '[attr.aria-pressed]': 'armed()', '(click)': 'toggle()', }, template: ` @if (url(); as url) { } @else { {{ asset().label }} } `, }) export class StampButton { readonly asset = input.required(); readonly armed = input(false); private readonly stamp = inject(EpdfStamp); protected readonly url = this.stamp.previewUrlOf(() => this.asset().id); protected toggle() { if (this.armed()) this.stamp.disarm(); else void this.stamp.armAsset(this.asset().id); } } @Component({ selector: 'demo-libraries', imports: [StampButton], changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (asset of assets(); track asset.id) { } @for (library of libraries(); track library.id) { } {{ restored() === null ? 'Restoring…' : restored() + ' restored: add a stamp, then reload the page' }}
`, }) export class Libraries { private readonly stamp = inject(EpdfStamp); protected readonly libraries = this.stamp.libraries; protected readonly assets = this.stamp.assetsOf(); protected readonly armedAsset = this.stamp.armedAsset; protected readonly restored = signal(null); constructor() { // Every change is written to the store from now on. inject(DestroyRef).onDestroy(persistStampLibraries(this.stamp, store)); // On load: what the store kept. The first visit starts a library of its own. void restoreStampLibraries(this.stamp, store).then(async (ids) => { this.restored.set(ids.length); if (ids.length > 0) return; const { library } = await this.stamp.createLibrary('My stamps'); await this.stamp.createAsset({ libraryId: library.id, label: 'Checked', mark: { kind: 'text', text: 'Checked', fontFamily: 'times-bold-italic', color: '#1f7a3f' }, }); }); } protected addStamp(libraryId: string) { const label = `Stamp ${this.assets().length + 1}`; void this.stamp.createAsset({ libraryId, label, mark: { kind: 'text', text: label, fontFamily: 'helvetica-bold', color: '#054fb3' }, }); } protected download(library: StampLibrary) { void this.stamp .exportLibrary(library.id) .then((bytes) => saveFile(bytes, `${library.name}.pdf`, 'application/pdf')); } } @Component({ selector: 'demo-root', imports: [ EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfSelectionLayer, EpdfAnnotationLayer, Libraries, ], providers: [ provideEmbedPdf( { engine, initialDocuments: [{ source: approvals }] }, withStage(), withRender(), withInteraction(), withSelection(), withAnnotation(), withStamp({ assetEngine }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './persist.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}