import { ChangeDetectionStrategy, Component, inject, 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 { displayNameOf, EpdfAttachment, withAttachment } from '@embedpdf/angular/attachment'; import type { AttachedFile } from '@embedpdf/angular/attachment'; import { localEngine } from '@embedpdf/engine'; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // The files the document carries: attach some, save one, remove one. @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withAttachment(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './files.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App { protected readonly attachment = inject(EpdfAttachment); protected readonly nameOf = displayNameOf; /** A file's size, as people read it. */ protected sizeOf(bytes: number | null): string { return bytes === null ? '' : bytes < 1024 ? `${bytes} B` : `${(bytes / 1024).toFixed(1)} KB`; } protected async save(file: AttachedFile) { const { bytes, name, mimeType } = await this.attachment.download(file); saveFile(bytes, name, mimeType ?? undefined); } protected attach(event: Event) { const input = event.target as HTMLInputElement; const picked = Array.from(input.files ?? []); input.value = ''; if (picked.length > 0) void this.attachment.create(picked.map((data) => ({ data }))); } }