import { ChangeDetectionStrategy, Component, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, EpdfDocuments, isPluginError, 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 { localEngine } from '@embedpdf/engine'; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // Read and select, but no downloading. const scope = ['doc.open', 'doc.render', 'doc.text.select']; /** What the last download did: the bytes, or why it was refused. */ type Result = | { kind: 'downloaded'; byteLength: number } | { kind: 'refused'; code: string; permission: string | null }; @Component({ selector: 'demo-download-anyway', changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (result(); as result) { @if (result.kind === 'downloaded') { Downloaded {{ result.byteLength }} bytes. } @else { Refused: {{ result.code }}, missing {{ result.permission }} } }
`, }) export class DownloadAnyway { private readonly documents = inject(EpdfDocuments); protected readonly result = signal(null); constructor() { // On load, the call the button makes, so the refusal shows at once. void this.download(); } protected async download() { try { const bytes = await this.documents.download(); this.result.set({ kind: 'downloaded', byteLength: bytes.byteLength }); } catch (error) { if (isPluginError(error, 'permission-denied')) { this.result.set({ kind: 'refused', code: error.code, permission: error.permission }); } } } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, DownloadAnyway], providers: [ provideEmbedPdf( { engine: () => localEngine(), scope, initialDocuments: [{ source: welcome }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './refused.css', encapsulation: ViewEncapsulation.None, template: `

Opening…

`, }) export class App {}