import { ChangeDetectionStrategy, Component, computed, inject, input, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, StageToken, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { EpdfCommand, EpdfCommands, standardCommands, withCommands, } from '@embedpdf/angular/commands'; import type { CommandDefinition } from '@embedpdf/angular/commands'; import { localEngine } from '@embedpdf/engine'; // A command of your own: pressed while the pages show two at a time. const twoPages: CommandDefinition = { id: 'layout:two-pages', label: 'Two pages', categories: ['layout'], active: ({ get }) => get(StageToken).getSettings().spread === 'odd', run: ({ get }) => { const stage = get(StageToken); stage.updateSettings({ spread: stage.getSettings().spread === 'odd' ? 'none' : 'odd' }); }, }; // [!doc-source welcome] 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()) }; }; // [!/doc-source] // Turning a category off hides its commands wherever they appear. @Component({ selector: 'label[demoCategorySwitch]', changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'switch' }, template: ` `, }) export class CategorySwitch { readonly category = input.required({ alias: 'demoCategorySwitch' }); private readonly commands = inject(EpdfCommands); protected readonly off = computed(() => this.commands.settings().disabledCategories.includes(this.category()), ); protected toggle(on: boolean) { if (on) this.commands.enableCategory(this.category()); else this.commands.disableCategory(this.category()); } } @Component({ selector: 'div[demoToolbar]', imports: [EpdfCommand, CategorySwitch], changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'toolbar' }, template: `
@for (id of buttons; track id) { }
`, }) export class Toolbar { protected readonly buttons = ['zoom:out', 'zoom:in', 'zoom:fit-page', 'layout:two-pages']; constructor() { // Run from code: the document opens with its whole first page in view. void inject(EpdfCommands).execute('zoom:fit-page'); } } @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, Toolbar], providers: [ provideEmbedPdf( { engine: () => localEngine(), initialDocuments: [{ source: welcome }] }, withStage(), withRender(), withCommands({ commands: [...standardCommands, twoPages] }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './custom.css', encapsulation: ViewEncapsulation.None, template: `

Loading…

`, }) export class App {}