import { ChangeDetectionStrategy, Component, DestroyRef, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, EpdfDocuments, provideEmbedPdf, saveFile, } from '@embedpdf/angular/runtime'; import type { OpenInput, PdfSaveMode } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { cloudEngine } from '@cloudpdf/engine'; const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' }; const megabytes = (bytes: Uint8Array) => `${(bytes.byteLength / 1_000_000).toFixed(2)} MB`; @Component({ selector: 'demo-download-bar', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (sizes(); as sizes) { Incremental {{ sizes.incremental }} · Rewrite {{ sizes.rewrite }} } @else { Measuring… }
`, }) export class DownloadBar { protected readonly documents = inject(EpdfDocuments); protected readonly mode = signal('incremental'); protected readonly sizes = signal | null>(null); constructor() { // How big each kind of download is: the same document, written two ways. const cancel = new AbortController(); inject(DestroyRef).onDestroy(() => cancel.abort()); Promise.all([ this.documents.download(undefined, { signal: cancel.signal }), this.documents.download(undefined, { mode: 'rewrite', signal: cancel.signal }), ]) .then(([incremental, rewrite]) => this.sizes.set({ incremental: megabytes(incremental), rewrite: megabytes(rewrite) }), ) .catch(() => {}); // cancelled: the example went away } protected async download() { saveFile(await this.documents.download(undefined, { mode: this.mode() }), 'welcome.pdf'); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, DownloadBar], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: welcome, name: 'welcome.pdf' }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './download.css', encapsulation: ViewEncapsulation.None, template: `

Opening…

`, }) export class App {}