import { ChangeDetectionStrategy, Component, inject, ViewEncapsulation } from '@angular/core'; import { EpdfDocument, EpdfDocumentGate, EpdfDocuments, 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()) }; }; @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome, name: 'welcome.pdf' }] }, withStage(), withRender(), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './open.css', encapsulation: ViewEncapsulation.None, template: `
@if (document.id()) { {{ document.name() ?? 'Untitled' }} {{ document.status() === 'ready' ? ' · ' + document.pageCount() + ' pages' : ' · ' + document.status() }} } @else { No document open } @if (document.id()) { } @else { }

{{ document.id() ? 'Opening…' : 'No document is open. Open a PDF to see it here.' }}

`, }) export class App { protected readonly documents = inject(EpdfDocuments); protected readonly document = inject(EpdfDocument); protected readonly welcome = welcome; // A file the user picks opens next to the welcome, and becomes the active document. protected async pick(picker: HTMLInputElement) { const file = picker.files?.[0]; picker.value = ''; if (!file) return; await this.documents.open( { kind: 'bytes', bytes: await file.arrayBuffer() }, { name: file.name }, ); } }